Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Using pivot in sql Expand / Collapse
Author
Message
Posted Monday, April 15, 2013 9:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 24, 2014 10:27 AM
Points: 7, Visits: 33
Hello,

I have a sql table as below


AutoId PID ItemId Item ItemValue Date
7560 432 1 Wbc 4 05/05/12 00:00
7561 432 2 HCT 4 05/05/12 00:00
7562 432 3 W/L/M 4 05/05/12 00:00
7563 432 4 Bilirubin 485 06/05/12 00:00
7564 432 1 Wbc 45 06/05/12 00:00
7565 432 2 HCT 4 06/05/12 00:00

I tried to use the pivot to display the result as below. But it didn't worked for me. Does any one have any idea on how to get this done?

AutoId PID ItemId Item 05/05/12 06/05/12
7560 432 1 Wbc 4
7561 432 2 HCT 4
7562 432 3 W/L/M 4
7563 432 4 Bilirubin 485
7564 432 5 Wbc 45
7565 432 6 HCT 4



Thanks,
VIjay
Post #1442355
Posted Monday, April 15, 2013 9:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:23 PM
Points: 13,272, Visits: 12,103
vijay-367524 (4/15/2013)
Hello,

I have a sql table as below


AutoId PID ItemId Item ItemValue Date
7560 432 1 Wbc 4 05/05/12 00:00
7561 432 2 HCT 4 05/05/12 00:00
7562 432 3 W/L/M 4 05/05/12 00:00
7563 432 4 Bilirubin 485 06/05/12 00:00
7564 432 1 Wbc 45 06/05/12 00:00
7565 432 2 HCT 4 06/05/12 00:00

I tried to use the pivot to display the result as below. But it didn't worked for me. Does any one have any idea on how to get this done?

AutoId PID ItemId Item 05/05/12 06/05/12
7560 432 1 Wbc 4
7561 432 2 HCT 4
7562 432 3 W/L/M 4
7563 432 4 Bilirubin 485
7564 432 5 Wbc 45
7565 432 6 HCT 4



Thanks,
VIjay


Check out the links in my signature about cross tabs. I suspect you will end up wanting to use the dynamic version.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1442357
Posted Monday, April 15, 2013 5:01 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:34 PM
Points: 36,952, Visits: 31,461
Am I the only one that doesn't think a Pivot is necessary here?

Never mind. The lack of indentation got me.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1442538
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse