本文介绍了如何将行转置为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SOHeaderId SODetailId
11004 100030
11004 100031
11004 100032
11004 100033
11004 100034
11004 100035
11004 100036
11005 100037
11005 100038
11005 100039
11005 100040
11006 100041
11006 100042
11006 100058
11006 100078
wanted it as
SOHeaderId SODetailId
11004 100030,100031,100032,100033,100034,100035,100036
11005 100037,100038,100039,100040
11006 100041,100042,100058,100078
WITH THE help of SQL Query only
我已经尝试过谷歌,但不能解决它。
I have already tried on google, but not able to solve it.
推荐答案
SELECT DISTINCT [SOHeaderId],
STUFF((SELECT ',' + CAST([SODetailId] AS VARCHAR(10))
FROM [TableName] t1
WHERE t1.[SOHeaderId] = t2.[SOHeaderId]
FOR XML PATH('')), 1, 1, '')
FROM [TableName] t2
我假设[SODetailId]的数据类型是 INT ,所以我将它转换为 VARCHAR(10)。
参考文献:
1. []
2. []
I am assuming that the data type of [SODetailId] is INT, so I am casting it to VARCHAR(10).
References:
1. STUFF AND FOR XML PATH for String Concatenation[^]
2. Using FOR XML PATH and STUFF to CONCATENATE[^]
这篇关于如何将行转置为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!