Can it change columns of a table values to rows

  • Hello,

    I have a table with this info:

    NrCard numberPersonAuto123456789101112

    11111111111111111111User1VW Jetta6,46,46,46,45,825,825,825,825,825,825,826,4

    22222222222222222222User2Honda CR-V 13,2113,2113,2112,0112,0112,0112,0112,0112,0112,0113,2113,21

    How I can get this result:

    NrCard numberPersonAutomonthvalue

    11111111111111111111User1VW Jetta16,4

    11111111111111111111User1VW Jetta26,4

    11111111111111111111User1VW Jetta36,4

    11111111111111111111User1VW Jetta45,82

    11111111111111111111User1VW Jetta55,82

    11111111111111111111User1VW Jetta65,82

    11111111111111111111User1VW Jetta75,82

    11111111111111111111User1VW Jetta85,82

    11111111111111111111User1VW Jetta95,82

    11111111111111111111User1VW Jetta105,82

    11111111111111111111User1VW Jetta116,4

    11111111111111111111User1VW Jetta126,4

    22222222222222222222User2Honda CR-V 113,21

    22222222222222222222User2Honda CR-V 213,21

    22222222222222222222User2Honda CR-V 312,01

    22222222222222222222User2Honda CR-V 412,01

    22222222222222222222User2Honda CR-V 512,01

    22222222222222222222User2Honda CR-V 612,01

    22222222222222222222User2Honda CR-V 712,01

    22222222222222222222User2Honda CR-V 812,01

    22222222222222222222User2Honda CR-V 912,01

    22222222222222222222User2Honda CR-V 1012,01

    22222222222222222222User2Honda CR-V 1113,21

    2704487352898000025/2User2Honda CR-V 1213,21

    Should I use unpivot or pivot?

  • You should use UNPIVOT or maybe the CROSS APPLY approach.

    http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks, I have done with unpivot, it is nice.

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

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