TSQL - 特定列的逗号分隔值
TSQL - Comma Separated Values for a specific column
我希望解决 SQL Table 具有以下 table 定义的问题:
UniqueID GroupID DepartmentID
-----------------------------------
1 2 D005
2 2 D006
3 2 D007
4 5 D002
5 1 D006
6 3 D001
7 3 D009
8 3 D002
9 3 D004
10 3 D006
这 table 存储属于特定组的不同部门的信息。
我正在寻找一个简单且优化的查询,它针对每个组将部门列表输出为逗号分隔值(在同一行中)
输出应该是这样的:
GroupID DepartmentList
-----------------------------
2 D005,D006,D007
5 D002
1 D006
3 D001,D009,D002,D004,D006
我使用了以下 table 定义。
IF EXISTS(SELECT * FROM [sys].[objects] WHERE [object_id]=OBJECT_ID(N'Table1') AND [TYPE]=N'U')
DROP TABLE Table1
;
CREATE TABLE Table1
(
[UniqueID] [int]
, [GroupID] [int]
, [DepartmentID] [varchar](4) NULL
);
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(1, 2, 'D005');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(2, 2, 'D006');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(3, 2, 'D007');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(4, 5, 'D002');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(5, 1, 'D006');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(6, 3, 'D001');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(7, 3, 'D009');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(8, 3, 'D002');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(9, 3, 'D004');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(10, 3, 'D006');
为了 运行 它在一个语句中我使用了 CTE with a select from values. Then I used the STUFF 函数来连接字符串值。
WITH
source_data
AS
(
SELECT Table1.* FROM (VALUES
( 1, 2, 'D005')
, ( 2, 2, 'D006')
, ( 3, 2, 'D007')
, ( 4, 5, 'D002')
, ( 5, 1, 'D006')
, ( 6, 3, 'D001')
, ( 7, 3, 'D009')
, ( 8, 3, 'D002')
, ( 9, 3, 'D004')
, ( 10, 3, 'D006')
) Table1 ([UniqueID], [GroupID], [DepartmentID])
)
SELECT DISTINCT
[GroupID]
, [DepartmentList] = STUFF
(
(
SELECT
',' + id2.[DepartmentID]
FROM
source_data AS id2
WHERE
id1.[GroupID] = id2.[GroupID]
GROUP BY
id2.[DepartmentID]
FOR XML PATH(''), TYPE
).value('.', 'varchar(max)')
,1,1,''
)
FROM
source_data AS id1
结果:
我希望解决 SQL Table 具有以下 table 定义的问题:
UniqueID GroupID DepartmentID
-----------------------------------
1 2 D005
2 2 D006
3 2 D007
4 5 D002
5 1 D006
6 3 D001
7 3 D009
8 3 D002
9 3 D004
10 3 D006
这 table 存储属于特定组的不同部门的信息。
我正在寻找一个简单且优化的查询,它针对每个组将部门列表输出为逗号分隔值(在同一行中) 输出应该是这样的:
GroupID DepartmentList
-----------------------------
2 D005,D006,D007
5 D002
1 D006
3 D001,D009,D002,D004,D006
我使用了以下 table 定义。
IF EXISTS(SELECT * FROM [sys].[objects] WHERE [object_id]=OBJECT_ID(N'Table1') AND [TYPE]=N'U')
DROP TABLE Table1
;
CREATE TABLE Table1
(
[UniqueID] [int]
, [GroupID] [int]
, [DepartmentID] [varchar](4) NULL
);
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(1, 2, 'D005');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(2, 2, 'D006');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(3, 2, 'D007');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(4, 5, 'D002');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(5, 1, 'D006');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(6, 3, 'D001');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(7, 3, 'D009');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(8, 3, 'D002');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(9, 3, 'D004');
INSERT INTO Table1 ([UniqueID], [GroupID], [DepartmentID]) VALUES(10, 3, 'D006');
为了 运行 它在一个语句中我使用了 CTE with a select from values. Then I used the STUFF 函数来连接字符串值。
WITH
source_data
AS
(
SELECT Table1.* FROM (VALUES
( 1, 2, 'D005')
, ( 2, 2, 'D006')
, ( 3, 2, 'D007')
, ( 4, 5, 'D002')
, ( 5, 1, 'D006')
, ( 6, 3, 'D001')
, ( 7, 3, 'D009')
, ( 8, 3, 'D002')
, ( 9, 3, 'D004')
, ( 10, 3, 'D006')
) Table1 ([UniqueID], [GroupID], [DepartmentID])
)
SELECT DISTINCT
[GroupID]
, [DepartmentList] = STUFF
(
(
SELECT
',' + id2.[DepartmentID]
FROM
source_data AS id2
WHERE
id1.[GroupID] = id2.[GroupID]
GROUP BY
id2.[DepartmentID]
FOR XML PATH(''), TYPE
).value('.', 'varchar(max)')
,1,1,''
)
FROM
source_data AS id1