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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 simple Query SUM by groups

Author  Topic 

andrewcw
Posting Yak Master

133 Posts

Posted - 2006-02-11 : 14:07:17
I can represent my table with this sample:

Item elapsedsecs
A 50
A 50
B 25
B 25
C 20

I try to get

A 100
B 50
C 20

with this query :
SELECT SUM(elapsedsecs) AS inTotal
FROM (SELECT item, elapsedsecs
FROM dbo.inworkTable
GROUP BY item, elapsedsecs) AS MY_AS

But instead i just get 1 column, with the sum = 95. This has 2 issues - it dumped the 'apparent duplicates' and did not do the grouping I had hoped and need.

I really need the grouping so I can run stdev and avg on the result.

What should the query be ?? Thanks

andrewcw

X002548
Not Just a Number

15586 Posts

Posted - 2006-02-11 : 14:18:55
SELECT item, SUM(elapsedsecs) AS SUM_elapsedsecs
FROM inworkTable
GROUP BY item

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

andrewcw
Posting Yak Master

133 Posts

Posted - 2006-02-11 : 14:27:37
Very Fast ! Thank you :)

andrewcw
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-13 : 01:57:04
Learn SQL
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -