多行到一个逗号分隔值不同的ID
Multiple rows to one comma-separated value different ID
This question already has an answer here:
Multiple rows to one comma-separated value [duplicate] 1 Answer
我想在 SQL 服务器中创建一个 table 值函数,我想 return 逗号分隔值中的数据。
例如table:tbl
ID | Value
---+-------
1 | 100
1 | 200
1 | 300
2 | 600
2 | 700
现在,当我在 SQL 服务器中执行查询时,我希望 table 像:
ID | Value
---+-------
1 | 100,200,300
2 | 600,700
CREATE TABLE #Table1
([ID] int, [Value] int)
;
INSERT INTO #Table1
([ID], [Value])
VALUES
(1, 100),
(1, 200),
(1, 300),
(2, 600),
(2, 700)
select id
,stuff((select ', ' + cast(value as varchar(10)) [text()]
from #table1
where id = t.id
for xml path(''), type)
.value('.','nvarchar(max)'),1,2,' ') value
from #table1 t
group by id
输出
ID Value
1 100, 200, 300
2 600, 700
使用 STUFF 方法获取结果:
CREATE TABLE #table ( ID INT, Value INT)
INSERT INTO #table ( ID , Value )
SELECT 1 , 100 UNION ALL
SELECT 1 , 200 UNION ALL
SELECT 1 , 300 UNION ALL
SELECT 2 , 600 UNION ALL
SELECT 2 , 700
SELECT ID , STUFF
(
(
SELECT ', ' + CAST(Value AS VARCHAR)
FROM #table Inr WHERE inr.ID = Otr.ID FOR XML PATH('')
),1,1,'')
FROM #table Otr
GROUP BY ID
测试数据
DECLARE @Table1 TABLE(ID INT, Value INT)
INSERT INTO @Table1 VALUES (1,100),(1,200),(1,300),(1,400)
查询
SELECT ID
,STUFF((SELECT ', ' + CAST(Value AS VARCHAR(10)) [text()]
FROM @Table1
WHERE ID = t.ID
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
FROM @Table1 t
GROUP BY ID
结果集
╔════╦═════════════════════╗
║ ID ║ List_Output ║
╠════╬═════════════════════╣
║ 1 ║ 100, 200, 300, 400 ║
╚════╩═════════════════════╝
This question already has an answer here:
Multiple rows to one comma-separated value [duplicate] 1 Answer
我想在 SQL 服务器中创建一个 table 值函数,我想 return 逗号分隔值中的数据。
例如table:tbl
ID | Value
---+-------
1 | 100
1 | 200
1 | 300
2 | 600
2 | 700
现在,当我在 SQL 服务器中执行查询时,我希望 table 像:
ID | Value
---+-------
1 | 100,200,300
2 | 600,700
CREATE TABLE #Table1
([ID] int, [Value] int)
;
INSERT INTO #Table1
([ID], [Value])
VALUES
(1, 100),
(1, 200),
(1, 300),
(2, 600),
(2, 700)
select id
,stuff((select ', ' + cast(value as varchar(10)) [text()]
from #table1
where id = t.id
for xml path(''), type)
.value('.','nvarchar(max)'),1,2,' ') value
from #table1 t
group by id
输出
ID Value
1 100, 200, 300
2 600, 700
使用 STUFF 方法获取结果:
CREATE TABLE #table ( ID INT, Value INT)
INSERT INTO #table ( ID , Value )
SELECT 1 , 100 UNION ALL
SELECT 1 , 200 UNION ALL
SELECT 1 , 300 UNION ALL
SELECT 2 , 600 UNION ALL
SELECT 2 , 700
SELECT ID , STUFF
(
(
SELECT ', ' + CAST(Value AS VARCHAR)
FROM #table Inr WHERE inr.ID = Otr.ID FOR XML PATH('')
),1,1,'')
FROM #table Otr
GROUP BY ID
测试数据
DECLARE @Table1 TABLE(ID INT, Value INT)
INSERT INTO @Table1 VALUES (1,100),(1,200),(1,300),(1,400)
查询
SELECT ID
,STUFF((SELECT ', ' + CAST(Value AS VARCHAR(10)) [text()]
FROM @Table1
WHERE ID = t.ID
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
FROM @Table1 t
GROUP BY ID
结果集
╔════╦═════════════════════╗
║ ID ║ List_Output ║
╠════╬═════════════════════╣
║ 1 ║ 100, 200, 300, 400 ║
╚════╩═════════════════════╝