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
 Transact-SQL (2000)
 Select with multiple SUM and restrictions?

Author  Topic 

zefiros
Starting Member

16 Posts

Posted - 2008-02-26 : 08:36:08
Hi,

I want perform a sum action on a table. To be more specific i want to output the SUM of each column of my
table with restrictions on each column

For instance

SELECT Descr, SUM(A) AS A
FROM dbo.TABLE
WHERE (A = 1)
GROUP BY Descr

Only that this apply only to column A, how would I do it for more columns and each column to have its own
comparison rules? The final result should look like this

Descr | A | B | C
Descr1 | 3 | 0 | 1
Descr2 | 0 | 2 | 1
Descr3 | 1 | 4 | 3

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-26 : 08:38:59

SELECT Descr,
SUM(case when A=1 then A else 0 end) AS A,
SUM(case when B=1 then B else 0 end) AS B,
SUM(case when C=1 then C else 0 end) AS C
FROM dbo.TABLE
GROUP BY Descr

Madhivanan

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

zefiros
Starting Member

16 Posts

Posted - 2008-02-26 : 12:36:32
thanks Madhivanan :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-27 : 01:42:36
Well. Read about Cross-tab reports for more informations

Madhivanan

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

- Advertisement -