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 |
|
cr488
Starting Member
23 Posts |
Posted - 2006-04-06 : 23:31:05
|
| I think I must be braindead today since I could not even firgue this out, so please help me on this. Thanks!My data set is like this (simple example, actually has more than 5000 records):table name: dataID type amt1 play 501 work 1002 work 1503 play 04 play 1005 play 205 work 30I am trying to do a self join to display data like this:ID play_amt work_amt1 50 1002 0 153 0 04 100 05 20 30So that I write my query like this:select d.id, d.amt as play_amt, d1.amt as work_amt from data d left outer join data d1 on d.id = d1.id and d1.type = 'work' where d.type = 'play' order by idI know this query is not working because it is missing some data... Can you help me on this? Thanks a lot!! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-06 : 23:36:12
|
[code]select ID, sum(case when type = 'play' then amt else 0 end) as play_amt, sum(case when type = 'work' then amt else 0 end) as work_amtfrom datagroup by ID[/code] KH |
 |
|
|
cr488
Starting Member
23 Posts |
Posted - 2006-04-07 : 00:58:29
|
Thanks for the quick reply. This is so simple and clean. I will try it out tomorrow. |
 |
|
|
|
|
|