SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Pivot Table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ssatjap1
Starting Member

4 Posts

Posted - 11/28/2011 :  10:42:28  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 11/28/2011 :  10:47:40  Show Profile  Reply with Quote

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


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

Go to Top of Page

ssatjap1
Starting Member

4 Posts

Posted - 11/28/2011 :  10:52:11  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 11/28/2011 :  10:54:58  Show Profile  Reply with Quote
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 - 11/28/2011 :  11:26:51  Show Profile  Reply with Quote
Thank you, I'll take a look at it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/28/2011 :  11:45:12  Show Profile  Reply with Quote
wc

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000