Unix Technical Forum

How to show field that not being group

This is a discussion on How to show field that not being group within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi all. I have this data: id GroupId DDate Value 1 1 7/31/2008 5 2 1 7/31/2008 6 3 ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-31-2008, 08:11 PM
Lemune
 
Posts: n/a
Default How to show field that not being group

Hi all.

I have this data:

id GroupId DDate Value
1 1 7/31/2008 5
2 1 7/31/2008 6
3 1 7/31/2008 2
4 2 7/31/2008 1
5 2 7/31/2008 2
6 2 7/31/2008 1

From this raw dataI want to show the data like this"
GroupId DDate TotalValue
1 7/31/2008 13
2 7/31/2008 4

current Query that I Use is

Select D.GroupId,(Select Top 1 DDate From MyTable Where
GroupId=D.GroupId)As DDate, Sum(D.Value) As TotalValue From MyTable D
Group By D.GroupId

But I want to remove the subquery, so my query will run faster.

Could it be done?

Thanks in Advanced
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-31-2008, 08:11 PM
Plamen Ratchev
 
Posts: n/a
Default Re: How to show field that not being group

You do not need the subquery, just group by both the groupid and ddate:

SELECT groupid, ddate, SUM(value) AS total_value
FROM MyTable
GROUP BY groupid, ddate;

If the date column has different values, then you can use MIN or MAX, based
on what makes sense:

SELECT groupid, MAX(ddate) AS ddate, SUM(value) AS total_value
FROM MyTable
GROUP BY groupid;


Plamen Ratchev
http://www.SQLStudio.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-31-2008, 08:11 PM
Lemune
 
Posts: n/a
Default Re: How to show field that not being group

Thanks Ratchev.

It' work boot of the method

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:05 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com