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 |
|
sadderson
Starting Member
13 Posts |
Posted - 2010-02-06 : 01:30:55
|
| SituationI have:open 01 10open 02 15open 03 5open 04 6close 01 5close 03 10close 05 15I need :Type 01 02 03 04 05 open 10 15 5 6 0close 5 0 10 0 15Each entry needs to be a field so i can create a report, so type 01, 02 03 04 and 05 are fields.ThanksScott |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-02-06 : 02:04:27
|
[code]DECLARE @sample TABLE( type varchar(5), col varchar(2), val int)INSERT INTO @sampleSELECT 'open', '01', 10 UNION ALLSELECT 'open', '02', 15 UNION ALLSELECT 'open', '03', 5 UNION ALLSELECT 'open', '04', 6 UNION ALLSELECT 'close', '01', 5 UNION ALLSELECT 'close', '03', 10 UNION ALLSELECT 'close', '05', 15SELECT *FROM @sample s pivot ( SUM(val) FOR col IN ([01], [02], [03], [04], [05]) ) p/*type 01 02 03 04 05 ----- ----------- ----------- ----------- ----------- ----------- close 5 NULL 10 NULL 15open 10 15 5 6 NULL(2 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sadderson
Starting Member
13 Posts |
Posted - 2010-02-06 : 03:04:55
|
| Sure, works like a charm... Thanks againScott right on the money.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-06 : 06:16:46
|
| if types are dynamic by any case you can use belowhttp://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx |
 |
|
|
|
|
|
|
|