我正在寻找一种分组方式,同时还将行连接成逗号分隔的字符串.示例:
I'm looking for a way to group whilst also concatenating rows into a comma separated string. Example:
Name Place
Steve Jones New York
Steve Jones Washington
Albert Smith Miami
Albert Smith Denver
到...
Steve Jones New York, Washington
Albert Smith Miami, Denver
非常感谢
如果您使用 SQL Server 2008 及更高版本,您可以使用 STUFF 和 XML PATH 来获得您想要的结果.
If you use SQL Server 2008 and above, you can use STUFF and XML PATH to get the result you want.
SELECT DISTINCT Name
, STUFF((
SELECT ',' + Place
FROM YourTable t1
where t1.Name = t2.Name
FOR XML PATH('')
), 1, 1, '') AS Places
FROM YourTable t2
这篇关于T-SQL 连接 &将多行分组为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
将每个子标记转换为具有多个分隔符的单列-SQLConverting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(将每个子标记转换为具有多个分隔符的单列-SQ
如何从多个表创建视图?How can I create a view from more than one table?(如何从多个表创建视图?)
根据前一行内的计算值创建计算值Create calculated value based on calculated value inside previous row(根据前一行内的计算值创建计算值)
如何将表格的前两列堆叠成一列,但也仅将第三How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何将表格的前两列堆
递归 t-sql 查询Recursive t-sql query(递归 t-sql 查询)
将月份名称转换为日期/月份编号(问题和答案的组Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(将月份名称转换为日期/月份编号(问题和答案的组合