Connecting Tech Pros Worldwide Help | Site Map

How to show field that not being group

  #1  
Old July 31st, 2008, 03:55 AM
Lemune
Guest
 
Posts: n/a
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
  #2  
Old July 31st, 2008, 04:25 AM
Plamen Ratchev
Guest
 
Posts: n/a

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

  #3  
Old July 31st, 2008, 04:25 AM
Lemune
Guest
 
Posts: n/a

re: How to show field that not being group


Thanks Ratchev.

It' work boot of the method

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error_reporting" setting not being recognized in my php.ini file laredotornado@zipmail.com answers 1 October 3rd, 2006 03:25 PM
form variable not being set yawnmoth answers 10 July 23rd, 2005 06:51 PM
Field not being updated within Stored Procedure AS400 Guru answers 1 July 23rd, 2005 09:58 AM
need to send email to 1000 addresses (currently in MS Excel)- what's the best way to do that, create a mail forma and paste ALL 1000 addresses in the TO field? NotGiven answers 14 July 17th, 2005 02:19 AM