none
how to create the csv column RRS feed

  • Question

  •  

    i want to create the csv col for the colname based on the below specified data

    table:

    id   colname   colval

    1    A             1

    2    B             1

    3    A             2

    4    B             2

    5    C             2

     

    i want the below format as o/p

    id  colname   colval

    1    A,B         1

    2    A,B,C     2

     

    please help me how to do this .............

     

     


    Sudhesh. G
    http://gurucoders.blogspot.com
    Tuesday, February 22, 2011 1:18 PM

Answers

  • CREATE  TABLE  #mable(mid INT, token nvarchar(16))

    INSERT INTO #mable VALUES (0, 'foo')
    INSERT INTO #mable VALUES(0, 'goo')
    INSERT INTO #mable VALUES(1, 'hoo')
    INSERT INTO #mable VALUES(1, 'moo')

    SELECT m1.mid,
           ( SELECT m2.token + ','
               FROM #mable m2
              WHERE m2.mid = m1.mid
              ORDER BY token
                FOR XML PATH('') ) AS token
      FROM #mable m1
     GROUP BY m1.mid ;


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Tuesday, February 22, 2011 1:24 PM
    Answerer

All replies

  • CREATE  TABLE  #mable(mid INT, token nvarchar(16))

    INSERT INTO #mable VALUES (0, 'foo')
    INSERT INTO #mable VALUES(0, 'goo')
    INSERT INTO #mable VALUES(1, 'hoo')
    INSERT INTO #mable VALUES(1, 'moo')

    SELECT m1.mid,
           ( SELECT m2.token + ','
               FROM #mable m2
              WHERE m2.mid = m1.mid
              ORDER BY token
                FOR XML PATH('') ) AS token
      FROM #mable m1
     GROUP BY m1.mid ;


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Tuesday, February 22, 2011 1:24 PM
    Answerer
  • thanks a lot URI Dimant  .this what i expected

    Sudhesh. G
    http://gurucoders.blogspot.com
    Tuesday, February 22, 2011 2:18 PM