我正在尝试阅读一个表格,用于市场篮分析。为此,我需要来自 SQL Server 2012 的销售订单行数据,格式如下:包含空格串联项目的单列。 例:
ordNo itemNo
x a1
x c2
y a1
y b4
y r1
到以下内容:
col0
a1 c2
a1 b4 r1
您可以使用FOR XML
子句:
select distinct stuff ((select distinct ' '+ t1.itemno
from table t1
where t1.ordno = t.ordno
for xml path('')
), 1, 1, ''
) as [col0]
from table t;