Using pivot in sql

  • Hello,

    I have a sql table as below

    AutoIdPIDItemIdItemItemValue 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?

    AutoIdPIDItemIdItem 05/05/1206/05/12

    7560 4321Wbc 4

    75614322 HCT 4

    75624323 W/L/M 4

    75634324 Bilirubin485

    7564 4325 Wbc 45

    7565 4326 HCT 4

    Thanks,

    VIjay

  • vijay-367524 (4/15/2013)


    Hello,

    I have a sql table as below

    AutoIdPIDItemIdItemItemValue 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?

    AutoIdPIDItemIdItem 05/05/1206/05/12

    7560 4321Wbc 4

    75614322 HCT 4

    75624323 W/L/M 4

    75634324 Bilirubin485

    7564 4325 Wbc 45

    7565 4326 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply