Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using pivot in sql


Using pivot in sql

Author
Message
vijay-367524
vijay-367524
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 54
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16669 Visits: 17030
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)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45420 Visits: 39942
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search