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 |
ssatjap1
Starting Member
4 Posts |
Posted - 2011-11-28 : 10:42:28
|
Hello Everyone,Sorry, I accidentally replied to an old post instead of posting to a new one.I'm very new to SQL, and I am trying to figure out how to pivot the following table:date ID Y/N2/1 1 Y2/2 2 N2/3 3 NULLetc..I want to keep the dates as rows, make the ID as columns, and the Y/N as the value. I have a very large data set, and I want to use the ID numbers as the column names.For example:Date 1 2 32/1 Y NULL NULL2/2 NULL N NULL2/3 NULL NULL NULLetc.I would appreciate anyone's help on this matter.Thank you. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-28 : 10:47:40
|
[code]select date,max(case when ID =1 then [Y/N] ELSE NULL END) AS [1],max(case when ID =2 then [Y/N] ELSE NULL END) AS [2],max(case when ID =3 then [Y/N] ELSE NULL END) AS [3]from tablegroup by date[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
ssatjap1
Starting Member
4 Posts |
Posted - 2011-11-28 : 10:52:11
|
Thank you for the fast reply!! Is it possible to not have to use the ID numbers as the column names without retyping all of the order id numbers?I have several thousand ids, and I want to keep the id numbers as they are. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
ssatjap1
Starting Member
4 Posts |
Posted - 2011-11-28 : 11:26:51
|
Thank you, I'll take a look at it. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-28 : 11:45:12
|
wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|