我有一个问题:我有两张桌子
Table1
它有两列
Col1 Col2
---- ------
a value1
b value1
b value1
和Table2
Col1 Col2
---- ------
1 a,b
2 a,c
3 a,b,c
我要结果
Col1 Col2
----- -----
a 1,2,3
b 1,3
c 2,3
我有一个问题:我有两张桌子
Table1
它有两列
Col1 Col2
---- ------
a value1
b value1
b value1
和Table2
Col1 Col2
---- ------
1 a,b
2 a,c
3 a,b,c
我要结果
Col1 Col2
----- -----
a 1,2,3
b 1,3
c 2,3
WITH C AS
(
SELECT T2.Col1,
S.Item
FROM Table2 AS T2
CROSS APPLY dbo.SplitStrings(T2.Col2, ',') AS S
)
SELECT C1.Item AS Col1,
(
SELECT ','+CAST(C2.Col1 AS VARCHAR(10))
FROM C AS C2
WHERE C1.Item = C2.Item
ORDER BY C2.Col1
FOR XML PATH(''), TYPE
).value('substring(text()[1], 2)', 'VARCHAR(MAX)') AS Col2
FROM C AS C1
GROUP BY C1.Item
尝试这个:
注意:未经测试
select col1, [col2],
(select col1+',' from Table2 where Col2=ID
group by col1 for xml path('')) AS Col2
From Table1