我需要组合1列相同的行,其余的类型为nvarchar
。
不是Temptable
Id | Key | Description1 | Description2
1 | 60287F58-4631| Lorem Ipsum | dolor sit amet
2 | 60287F58-4631| consectetur | adipiscing elit
3 | 74553234-7543| Test test | test test
我需要得到这个结果:
Key | DescriptionCombined
60287F58-4631| Lorem Ipsum, dolor sit amet, consectetur, adipiscing elit
74553234-7543| Test test, test test
我知道group by
无效,因为Description1
和Description2
在公共行之间有所不同。我开始尝试使用for xml path('')
,但这会结合每一行。我想我需要首先使用相同的密钥提取组,然后应用for xml path('')
,但我无法使其工作。
select ',' + t1.Description1, t1.Description2 from TempTable t1
for xml path('')
答案 0 :(得分:1)
我认为您可以将子查询与FOR XML PATH('')
SELECT
[Key],
STUFF(
(
SELECT ', '+Description1+', '+Description2
FROM TempTable t2
WHERE t2.[Key]=t1.[Key]
ORDER BY t2.Id
FOR XML PATH('')
),1,2,'') DescriptionCombined
FROM (SELECT DISTINCT [Key] FROM TempTable) t1
答案 1 :(得分:0)
您可以在CTE中使用STUFF功能
CREATE TABLE #TEMP (ID INT,[KEY] NVARCHAR(50),Description1 NVARCHAR(50),Description2 NVARCHAR(50))
INSERT INTO #TEMP
SELECT 1,'60287F58-4631','Lorem Ipsum','dolor sit amet' UNION ALL
SELECT 2,'60287F58-4631','consectetur','adipiscing elit' UNION ALL
SELECT 3,'74553234-7543','Test test','test test'
;WITH CTE
AS
( SELECT [Key],
STUFF(
(
SELECT ', '+Description1+', '+Description2
FROM #TEMP a
WHERE a.[Key]=b.[Key]
ORDER BY a.Id
FOR XML PATH('')
),1,2,'') DescriptionCombined
FROM (SELECT DISTINCT [Key] FROM #TEMP) b
)
SELECT * FROM CTE
答案 2 :(得分:0)
您可以使用OUTER APPLY
DECLARE @TempTable TABLE(Id INT, [Key] VARCHAR(20), Description1 VARCHAR(20) , Description2 VARCHAR(20) )
INSERT INTO @TempTable VALUES
( 1 ,'60287F58-4631', 'Lorem Ipsum', 'dolor sit amet'),
( 2 ,'60287F58-4631', 'consectetur', 'adipiscing elit'),
( 3 ,'74553234-7543', 'Test test', 'test test')
select [Key], MAX(STUFF(X.DescriptionCombined,1,1,'')) DescriptionCombined
FROM @TempTable T1
OUTER APPLY ( SELECT ', ' + Description1 +', '+ Description2
FROM @TempTable T2 WHERE T1.[Key] = T2.[Key] FOR XML PATH('') ) X ( DescriptionCombined )
GROUP BY [Key]
结果:
Key DescriptionCombined
-------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
60287F58-4631 Lorem Ipsum, dolor sit amet, consectetur, adipiscing elit
74553234-7543 Test test, test test
答案 3 :(得分:0)
只是为了好玩 - 采用递归CTE方法:
;WITH cte AS (
SELECT TOP 1 WITH TIES id,
[Key],
CAST(CONCAT(Description1,', ',Description2) as nvarchar(max))as DescriptionCombined,
1 as lev
FROM YourTable y
ORDER BY ROW_NUMBER() OVER (PARTITION BY [Key] ORDER BY id ASC)
UNION ALL
SELECT y.id,
c.[Key],
CAST(CONCAT(c.DescriptionCombined,', ',Description1,', ',Description2) as nvarchar(max))as DescriptionCombined,
lev+1
FROM YourTable y
INNER JOIN cte c
ON y.[Key] = c.[key] AND y.[id] > c.[id]
)
SELECT TOP 1 WITH TIES [Key],
DescriptionCombined
FROM cte
ORDER BY ROW_NUMBER() OVER (PARTITION BY [Key] ORDER BY lev DESC)
输出:
Key DescriptionCombined
60287F58-4631 Lorem Ipsum, dolor sit amet, consectetur, adipiscing elit
74553234-7543 Test test, test test