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.
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 mytable with restrictions on each columnFor instanceSELECT Descr, SUM(A) AS AFROM dbo.TABLEWHERE (A = 1)GROUP BY DescrOnly that this apply only to column A, how would I do it for more columns and each column to have its owncomparison rules? The final result should look like thisDescr | A | B | CDescr1 | 3 | 0 | 1Descr2 | 0 | 2 | 1Descr3 | 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 CFROM dbo.TABLEGROUP BY DescrMadhivananFailing to plan is Planning to fail |
 |
|
zefiros
Starting Member
16 Posts |
Posted - 2008-02-26 : 12:36:32
|
thanks Madhivanan :) |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-27 : 01:42:36
|
Well. Read about Cross-tab reports for more informationsMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|