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 |
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2011-01-27 : 14:10:11
|
I have table in the following formatcreate table #test(id int , amount int, flag int, code varchar(2))insert into #test (id,amount,flag,code)select 1,10, 9, 'x' union all select 1,10, 9, 'y' union allselect 2,20, 9, 'x' union all select 2,20, 8, 'y' union allselect 3,30, 8, 'z'select * from #testid amount flag code----------- ----------- ----------- ----1 10 9 x1 10 9 y2 20 9 x2 20 8 y3 30 8 zI need to add up the amounts across the IDs at codelevel.SELECT distinct code, SUM(isnull(Amount,0)) Amount from #test GROUP BY codecode Amount---- -----------x 30y 30z 30Now i need to filter out amounts related to a certain flag.I need to add an where condition to consider amount = 0 where flag = 8my final out put should be.Code Amount----------- -----------X 30Y 10X 0Please help me out on how to handle this scenario. Thanks in advance. |
|
ZZartin
Starting Member
30 Posts |
Posted - 2011-01-27 : 15:05:10
|
Try this for the SUMSUM(isnull(CASE WHEN flag = 8 THEN 0 ELSE Amount END,0)) |
 |
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2011-01-27 : 17:12:55
|
thanks ZZartin it worked |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-01-28 : 01:03:33
|
orSUM(CASE WHEN flag = 8 THEN 0 ELSE Amount END,0)MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|