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)
 SQL help combining DISTINCT and SUM

Author  Topic 

pamschle
Starting Member

2 Posts

Posted - 2005-10-11 : 20:51:04
Need help building a SQL statement that will return the SUM of colB from a database table for unique values of colA.

For instance:

select DISTINCT colA, SUM(colB) from tableA

Doesn't work, but it's the right idea.

Any help would be appreciated.

Thanks - Paula

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-10-11 : 20:57:35
You don't want DISTINCT, but rather GROUP BY -- read up on it in books on-line or in a decent beginning SQL book. It is a very powerful feature.

SELECT ColA, SUM(ColB)
FROM TableA
GROUP BY ColA
Go to Top of Page

Hippi
Yak Posting Veteran

63 Posts

Posted - 2005-10-11 : 20:59:30
How about
select colA, sum(colB) from Table
group by colA

Hippi
Go to Top of Page

pamschle
Starting Member

2 Posts

Posted - 2005-10-12 : 09:02:00
That suggestion returns all the distinct rows, I only want to return the sum - result set should be one row, one column.

Thanks! Paula
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-10-12 : 09:06:12
can you post some data .. how you want ???

Complicated things can be done by simple thinking
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-10-12 : 09:12:29
select sum(colb)
from table
Go to Top of Page
   

- Advertisement -