Home Forums SQL Server 2008 T-SQL (SS2K8) manipulate multiple row data into single row concatinated RE: manipulate multiple row data into single row concatinated

  • You can concatenate using 'FOR XML PATH...'

    DECLARE @mytable TABLE(PolNo INT, [YEAR] INT, Platform VARCHAR(3), Number INT, Record INT, memo VARCHAR(30))

    INSERT INTO @mytable(PolNo, [YEAR], Platform, Number, Record, memo)

    VALUES

    (123,2010,'pc',1,1,'The user had issues'),

    (123,2010,'pc',1,2,'with the os.'),

    (123,2009,'pc',2,1,'Replaced RAM'),

    (123,2010,'mac',1,1,'Ordered new CDs'),

    (564,2009,'mac',1,1,'Broken CD TRAY'),

    (564,2010,'mac',1,1,'USB port dead'),

    (564,2010,'pc',1,1,'Ordered replacement'),

    (564,2010,'pc',1,2,'laptop');

    SELECT CAST(t.PolNo AS VARCHAR(10)) + '-' + CAST(t.[YEAR] AS VARCHAR(10)) + '-' + t.Platform + '-' + CAST(t.Number AS VARCHAR(10)) AS ID,

    (SELECT t2.memo + ' ' AS "text()"

    FROM @mytable t2

    WHERE t2.PolNo = t.PolNo

    AND t2.[YEAR] = t.[YEAR]

    AND t2.Platform = t.Platform

    AND t2.Number = t.Number

    ORDER BY t2.Record

    FOR XML PATH(''),TYPE).value('./text()[1]','VARCHAR(8000)') AS MEMO

    FROM @mytable t

    GROUP BY t.PolNo, t.[YEAR],t.Platform, t.Number

    ORDER BY ID;

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537