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 2005 Forums
 Transact-SQL (2005)
 Help creating PIVOT statement

Author  Topic 

CXXXV
Starting Member

12 Posts

Posted - 2009-06-17 : 15:51:50
I need to create a statement that will pivot the data shown in the table to result like that shown.

Table looks like this:

DATE......MEMBER_ID.....QTY.....ENSURE
1/1/2009.....271..............1.......null
1/2/2009.....271..............1.......null
1/3/2009.....271..............1.......5
1/4/2009.....271..............1.......null
1/5/2009.....271..............1.......null

Need results to look like this:

MEMEBER_ID...1/1/2009...1/2/2009...1/3/2009...1/4/2009...1/5/2009
271...................1.................1...............1..........1...........1
271...................1.................1...............5..........1...........1

In essence I need two PIVOT queries in one statement. The first returns a PIVOT for QTY and the second a PIVOT for ENSURE.

The rub is that I need two rows in the result set. One from each PIVOT grouped by MEMBER_ID.

(Sorry for the format. Couldn't figure out anyway to paste an image in here)

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-17 : 15:55:07
If the dates are not fixed, you will need to resort to dynamic sql. Twice.

Once for the Ensure sum, and once for the Qty sum.
Store the two intermediate results in a temporary table and then select from this table.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -