将多行连接到一个字段中

时间:2013-12-20 08:52:38

标签: sql-server tsql

我有一个包含三列像image(Source)的表,想要编写一个像图像一样的表(Result)

enter image description here

1 个答案:

答案 0 :(得分:6)

SELECT DISTINCT customer,
    stuff((
            SELECT ',' + cast(policy as varchar(10))
            FROM table1 b
            WHERE a.customerid = b.customerid
            FOR XML path('')
            ), 1, 1, '') [policies]
FROM table1 a