使用逗号分隔值分组并排除前一列值中的值
Group by with comma-separated values and excluding the value from the previous column value
我有两个表 Lecturer
和 Student
,我想在新列中用逗号分隔值显示其他讲师 ID。我正在使用 SQL Server 2014。
Table:讲师
id name subject
-------------------------------
102 Mark Designing
103 John Analytics
104 Jacob Science
Table:学生讲师
id Fname Lname Lid
--------------------------------
1 Jack P 102
1 Jack P 103
1 Jack P 104
通过使用 group by 我得到如下的单个值:
SELECT id, fname, lname, lid
FROM studentlecturer
GROUP BY id
Table:学生讲师
id Fname Lname Lid
--------------------------------
1 Jack P 102
预期结果
id Fname Lname Lid Concat Values
---------------------------------------------------
1 Jack P 102 103,104
1 Jack P 103 102,104
1 Jack P 104 102,103
SQL Fiddle: http://sqlfiddle.com/#!7/73304
您可以使用 for xml
来汇总您的值。 Stuff
删除开头的逗号。
select *
from StudentLecturer s
outer apply (
select ConcatValues=Stuff((select ',' + Cast(l.Id as varchar(10))
from Lecturer l where l.id != s.Lid
for xml path('')),1,1,'')
)l
这可以使用 For XML Path("), TYPE
完成,如下所示:
SELECT S.id, S.Fname, S.Lname, S.Lid,
STUFF((SELECT Concat(',',Lid) FROM StudentLecturer WHERE id=S.id And Lid<>S.Lid
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)'),1,1,'') AS [Concat Values]
FROM StudentLecturer As S
对 SQL Server 2017 (14.x) 及更高版本使用 String_Agg
函数。
SELECT S.id, S.Fname, S.Lname, S.Lid,
STRING_AGG(S_1.Lid, ',') WITHIN GROUP (ORDER BY S_1.Lid) AS [Concat Values]
FROM StudentLecturer AS S LEFT JOIN StudentLecturer AS S_1 ON (S.id=S_1.id AND
S.Lid<>S_1.Lid)
GROUP BY S.id, S.Fname, S.Lname, S.Lid
ORDER BY S.Fname, S.Lname, S.Lid
我有两个表 Lecturer
和 Student
,我想在新列中用逗号分隔值显示其他讲师 ID。我正在使用 SQL Server 2014。
Table:讲师
id name subject
-------------------------------
102 Mark Designing
103 John Analytics
104 Jacob Science
Table:学生讲师
id Fname Lname Lid
--------------------------------
1 Jack P 102
1 Jack P 103
1 Jack P 104
通过使用 group by 我得到如下的单个值:
SELECT id, fname, lname, lid
FROM studentlecturer
GROUP BY id
Table:学生讲师
id Fname Lname Lid
--------------------------------
1 Jack P 102
预期结果
id Fname Lname Lid Concat Values
---------------------------------------------------
1 Jack P 102 103,104
1 Jack P 103 102,104
1 Jack P 104 102,103
SQL Fiddle: http://sqlfiddle.com/#!7/73304
您可以使用 for xml
来汇总您的值。 Stuff
删除开头的逗号。
select *
from StudentLecturer s
outer apply (
select ConcatValues=Stuff((select ',' + Cast(l.Id as varchar(10))
from Lecturer l where l.id != s.Lid
for xml path('')),1,1,'')
)l
这可以使用 For XML Path("), TYPE
完成,如下所示:
SELECT S.id, S.Fname, S.Lname, S.Lid,
STUFF((SELECT Concat(',',Lid) FROM StudentLecturer WHERE id=S.id And Lid<>S.Lid
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)'),1,1,'') AS [Concat Values]
FROM StudentLecturer As S
对 SQL Server 2017 (14.x) 及更高版本使用 String_Agg
函数。
SELECT S.id, S.Fname, S.Lname, S.Lid,
STRING_AGG(S_1.Lid, ',') WITHIN GROUP (ORDER BY S_1.Lid) AS [Concat Values]
FROM StudentLecturer AS S LEFT JOIN StudentLecturer AS S_1 ON (S.id=S_1.id AND
S.Lid<>S_1.Lid)
GROUP BY S.id, S.Fname, S.Lname, S.Lid
ORDER BY S.Fname, S.Lname, S.Lid