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 |
|
kafkas
Starting Member
2 Posts |
Posted - 2010-04-07 : 10:45:53
|
| Hi allI have a tableID-----TOTAL-----DIV________________________1----- 10----- ST11----- 10----- ST21----- 10----- ST21----- 10----- ST31----- 10----- AT12----- 10----- ST12----- 10----- ST12----- 10----- ST1and i want this.ID----- ST----- AT_________________________________________________1----- 40----- 102----- 30----- 0I trygroup by, sum,left and case when. but i can't figure it out.Could you please help?Any help would be appreciated.Thanks in advance. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-07 : 11:03:49
|
[code]select ID,sum(case when left(Div,2)='ST' then TOTAL else 0 end) as ST,sum(case when left(Div,2)='AT' then TOTAL else 0 end) as ATfrom-- making testdata in a derived table called dt-- instead of this place your_table here(select 1 as ID, 10 as Total, 'ST1' as Div union allselect 1,10,'ST2' union allselect 1,10,'ST2' union allselect 1,10,'ST3' union allselect 1,10,'AT1' union allselect 2,10,'ST1' union allselect 2,10,'ST1' union allselect 2,10,'ST1')dtgroup by ID[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
kafkas
Starting Member
2 Posts |
Posted - 2010-04-07 : 11:24:27
|
| Thank you very much really.i was misplacing SUM like,case when left(Div,2)='AT' then sum(TOTAL) else 0 end) as ATyou are the man. thanks a lot. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-07 : 11:32:36
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|