Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sum query?

Author  Topic 

sqlneve
Starting Member

16 Posts

Posted - 2008-11-20 : 17:34:04
i have a query that works well enough as follows

SELECT SUM (MAX_MONTHL)
--SELECT *
FROM SDE.TEST
WHERE SURVEY = 'wallace, l'

but i have almost 200 different surveys to run it on... it there a better way to do this rather than one by one? Is there a way to say where survey name is unique and get an output of all surveys with their sums? or am i just being crazy?

nr
SQLTeam MVY

12543 Posts

Posted - 2008-11-20 : 17:41:20
select SURVEY, SUM (MAX_MONTHL)
from SDE.TEST
group by SURVEY

Could put in a where clause if you don't want all surveys

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-20 : 17:41:54
Umm.. how about:

SELECT SUM (MAX_MONTHL)
FROM SDE.TEST
GROUP BY SURVEY
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-21 : 01:56:19
quote:
Originally posted by DavidChel

Umm.. how about:

SELECT SUM (MAX_MONTHL)
FROM SDE.TEST
GROUP BY SURVEY


no probelm but only thing is you need to include the grouped field as well in select else you wont be able to identify which sum value corresponds to which SURVEY. See previous solution provided.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2008-11-21 : 02:55:35
And that's a post that adds little to the thread
Just like this one.

(sorry I'm drunk)



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-21 : 09:20:05
quote:
Originally posted by visakh16

quote:
Originally posted by DavidChel

Umm.. how about:

SELECT SUM (MAX_MONTHL)
FROM SDE.TEST
GROUP BY SURVEY


no probelm but only thing is you need to include the grouped field as well in select else you wont be able to identify which sum value corresponds to which SURVEY. See previous solution provided.



Oops, duh.
Go to Top of Page

sqlneve
Starting Member

16 Posts

Posted - 2008-11-21 : 09:22:51
thanks everyone... this really helped me out!
Go to Top of Page

NeilG
Aged Yak Warrior

530 Posts

Posted - 2008-11-21 : 09:30:28
Your only saving grace on this one nigel is that the time is showen as 24hrs otherwise i would be worried at you being drunk at 2:55 in the afternoon :-)
Go to Top of Page
   

- Advertisement -