none
without PIVOT Function?

    Question

  • CREATE TABLE SIMPLE(NAME varchar(3),
    DEC VARCHAR(3))

    INSERT INTO SIMPLE VALUES ('w1','A')
    INSERT INTO SIMPLE VALUES ('w1','B')
    INSERT INTO SIMPLE VALUES ('w1','C')
    INSERT INTO SIMPLE VALUES ('w2','A')
    INSERT INTO SIMPLE VALUES ('w2','B')

    SELECT * FROM SIMPLE


    i need O/P like this
    name     Dec
    ------  ------
    w1       A,B,C
    W2       A,B   


    santoshbangalore

    Tuesday, August 13, 2013 4:13 AM

Answers

  • Try this,

    SELECT [NAME], 
           Stuff((SELECT ',' + [DEC] 
                  FROM   [SIMPLE]
                  WHERE  [NAME] = a.NAME 
                  FOR xml path('')), 1, 1, '') [DEC]
    FROM   [SIMPLE] a 
    GROUP  BY  [NAME]


    Regards, RSingh

    Tuesday, August 13, 2013 4:55 AM
  • Please try this

    SELECT  s.NAME ,
            STUFF(( SELECT  ',' + DEC
                    FROM    SIMPLE s2
                    WHERE   s2.NAME = s.NAME
                  FOR
                    XML PATH('')
                  ), 1, 1, '') AS columns2
    FROM    SIMPLE s
    GROUP BY s.NAME


    If This post is helpful post, please vote it. If This post is the answer to your question, please Propose it as answer. Thanks so much for your feedback.

    Tuesday, August 13, 2013 5:00 AM

All replies