Multpile rows in a single row

  • Hi i got a table which has data like

    Table

    Col1----col2----Col3

    X--------1-------a

    X--------1--------b

    X--------1--------c

    Y--------2-------e

    Y--------2--------f

    Z--------2-------g

    Z--------2-------h

    i should get an out put which looks like

    col1---col2---col3

    x------1------a,b,c

    y------2-------e,f

    z------2--------g,h

    Can any one help me out with this...

    Thank you

  • tripri (8/23/2010)


    Hi i got a table which has data like

    Table

    Col1----col2----Col3

    X--------1-------a

    X--------1--------b

    X--------1--------c

    Y--------2-------e

    Y--------2--------f

    Z--------2-------g

    Z--------2-------h

    i should get an out put which looks like

    col1---col2---col3

    x------1------a,b,c

    y------2-------e,f

    z------2--------g,h

    Can any one help me out with this...

    Thank you

    Is this homework? what do you got so far?

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • Try this....

    SELECT DISTINCT c1.Col1, c1.Col2,

    STUFF((SELECT ',' + Col3 FROM Tablename c WHERE c.Col1 = c1.Col1 FOR XML PATH('')),1,1,'')

    FROM Tablename c1

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

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