【发布时间】:2020-12-08 11:25:53
【问题描述】:
我正在尝试通过下面的这个 sql Select 语句,但我无法正确完成。
Select DISTINCT vc.cid, vt.tid, vc.device,
STUFF((select ', ' + c.tName from thumbTbl t2
join tags v ON t2.cid = v.cid
join config c on v.tid = c.tid
where vc.cid = t2.cid
group by c.tName
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(max)'), 1, 1, '') tName
from tags vt
Inner join thumbTbl vc on vt.cid = vc.cid
Left join config vtc on vt.tid = vtc.tid
order by vc.cid asc
结果如下:
cid tid device tName
--- --- --------- ---------
159 7 Mat Dwens Escalation, Follow Up, More Benefits
159 11 Mat Dwens Escalation, Follow Up, More Benefits
159 12 Mat Dwens Escalation, Follow Up, More Benefits
160 7 Jeniffer P Rectro
162 8 Marc Novice More Benefits, Rectro
162 6 Marc Novice More Benefits, Rectro
165 4 Jeniffer P Follow up
问题:如何只显示每个 Selected Id 的 1 行?,我尝试使用 SELECT DISTINCT 但仍然给了我相同的结果。
我想看到的结果应该是这样的:
cid tid device tName
------- --------- ----------
159 12 Mat Dwens Escalation, Follow Up, More Benefits
160 7 Jeniffer P Rectro
162 8 Marc Novice More Benefits, Rectro
165 4 Jeniffer P Follow up
有人有想法吗?
【问题讨论】:
-
请提供样本数据和期望的结果。
-
@GordonLinoff 只需添加示例数据,谢谢
标签: sql sql-server distinct for-xml-path select-for-xml