need query unpivot in sql servr

  • Hi friends i have small doubt in sql server please tell me how to solve this issuse in sql server.

    table:patient

    pn | cpt1 |cpt2 | cpt3| modi12 | modi13 | modi22 | modi33 | mod31|modi11

    1 |100 |200 |300 | a | |g | k | v |q

    2 |101 |400 |450 | h | b | | | m |

    Here modi13 means modfiere1 with cpt3

    modi12 means modifier1 with cpt2

    modi22 means modifier2 with cpt2

    modi33 means modifier3 with cpt3

    modi31 means modifier3 with cpt1

    modi11 means modifier1 with cpt1

    modi13 means modifier1 with cpt3

    based on this condition i need output like this

    pn | cpt | modifier1 | modifier2 | modifier3

    1 | 100 | q | |v

    2 | 101 | | |m

    1 | 200 | a | g |

    2 | 400 | h | |

    1 | 300 | | |k

    2 | 450 | b | |

    i tried cpt1,cpt2,cpt3 information stored in cpt useing unpivot.but here problem is how i displaay corresponding modfier1 and modifier2 and modifier3 values information.

    select cpt from

    ( select pn,cpt1,cpt2,cpt3 from patient)main

    unpivot

    ( cpt for cptvalues in (cpt1,cpt2,cpt3))sub that time i get only upto pn corresponding cpt values.

    please tell me query how to solve this issue in sql server .

  • Step 1 would be to make it easy for someone to give you a working, tested, coded solution. Please see the first link under "Helpful Links" in my signature line below for how to do that.

    --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 2 posts - 1 through 1 (of 1 total)

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