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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Pivot Table

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/N
2/1 1 Y
2/2 2 N
2/3 3 NULL
etc..

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 3
2/1 Y NULL NULL
2/2 NULL N NULL
2/3 NULL NULL NULL

etc.

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 table
group by date
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-28 : 10:54:58
quote:
Originally posted by ssatjap1

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.


its possible. but you need this

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ssatjap1
Starting Member

4 Posts

Posted - 2011-11-28 : 11:26:51
Thank you, I'll take a look at it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-28 : 11:45:12
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -