【发布时间】:2015-01-07 06:55:29
【问题描述】:
使用此页面:Can I Comma Delimit Multiple Rows Into One Column?
...我已经能够得出这样的结果:
使用这个查询:
SELECT
[Client ID],
STUFF((SELECT ', ' + [Location (counts)]
FROM (
SELECT TOP 100 PERCENT [Client ID]
, ltrim(str([Store Num])) + ' (' + ltrim(str(count([Store Num]))) + ') ' [Location (counts)]
FROM @tbl_coreData
GROUP BY [Store Num], [Client ID]
ORDER BY COUNT([Store Num]) DESC) tblThis
WHERE ([Store Num] = Result.[Store Num]
AND [Client ID] = Result.[Client ID])
FOR XML PATH ('')),1,1,'') AS BATCHNOLIST
FROM @tbl_coreData AS Result
GROUP BY [Client ID], [Store Num]
我希望列表按出现次数(括号中的数字)排序。我的尝试是
SELECT TOP 100 PERCENT
[Client ID]
和
ORDER BY COUNT([Store Num]) DESC
但这不起作用。
如何使连接的值按出现的降序显示?
一如既往,欢迎并感谢所有帮助。
【问题讨论】:
标签: sql sql-server tsql subquery concatenation