我有这张桌子: T:
d
我要做的是在一个单元格(合并单元格)中获取每个s_id
所有符号(DBSymbol
)。
我找到了this教程,这是我的代码:
select T.s_id,
(select '; ' + D.symbol
from D
where T.D_b_id = D.id
FOR XML PATH('')) [DBSymbol]
from T
但这是我得到的:
有什么问题?
答案 0 :(得分:1)
select DISTINCT T.s_id,
Stuff((SELECT DISTINCT '; ' + D.symbol
from D
--where T.D_b_id = D.id
FOR XML PATH('')),1,1,'') [DBSymbol]
from T
示例here
答案 1 :(得分:1)
试试这个 -
SELECT t1.s_id,
STUFF(
(SELECT '; ' + symbol AS [text()]
FROM (
SELECT t.s_id,
d.symbol
FROM T
INNER JOIN D ON T.d_b_id = D.id
WHERE t.s_id = t1.s_id
) x
FOR XML PATH('')
), 1, 1, '')
FROM T t1
GROUP BY t1.s_id
检查:SQL Fiddle