来自连接表内部查询的逗号分隔字符串 (SQL Server)
我搜索了很多帖子、网站、博客以及 API,但似乎无法弄清楚这一点。我想从连接表中的行创建 CSV 字符串,但有一个问题。它需要针对整组数据。我知道,令人困惑,这是故障。
让我们使用下表中的通用示例:
- Students (ID、Name、Gpa)
- Classes (ID、Dept、Level)
- StudentsInClasses (StudentID、ClassID)
假设我想查询所有班级,但在该查询内(针对每条记录)我还想获取每个班级的学生 ID 的 CSV 字符串。结果应该如下所示:
Class StudentsID_CSVString
----- --------------------
BA302 1,2,3,4,5,6,7,8,9
BA479 4,7,9,12,15
BA483 7,9,12,18
我尝试使用如下所示的合并语句,但由于合并语法愚蠢,我无法让它工作。有没有更好的方法来实现这个目标,或者我只是犯了一个愚蠢的语法错误?
declare @StudentsID_CSVString varchar(128)
select Dept + Level as 'Class',
coalesce(@StudentsID_CSVString + ',', '')
+ CAST(StudentID as varchar(8)) as 'StudentsID_CSVString'
from Classes
left outer join StudentsInClasses ON Classes.ID = StudentsInClasses.ClassID
我使用以下代码进行测试:
declare @Students table (ID int, Name varchar(50), Gpa decimal(3,2))
declare @Classes table (ID int, Dept varchar(4), [Level] varchar(3))
declare @StudentsInClasses table (StudentID int, ClassID int)
declare @StudentsID_CSVString varchar(128)
insert into @Students (ID) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18)
insert into @Classes (ID, Dept, [Level]) values (1, 'BA', '302'), (2, 'BA', '379'), (3, 'BA', '483')
insert into @StudentsInClasses (StudentID, ClassID) values
(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),(9,1),
(4,2),(7,2),(9,2),(12,2),(15,2),
(7,3),(9,3),(12,3),(18,3)
I've searched many threads, sites, and blogs, as well as the API and can't seem to figure this one out. I want to create a CSV string from rows in a junction table, but here's the catch. It needs to be for an entire set of data. I know, confusing, here's the breakdown.
Let's use the generic example with the following tables:
- Students (ID, Name, Gpa)
- Classes (ID, Dept, Level)
- StudentsInClasses (StudentID, ClassID)
Say I want to query for all classes, but within that query (for each record) I want to also get a CSV string of the Student IDs in each of the classes. The results should look something like this:
Class StudentsID_CSVString
----- --------------------
BA302 1,2,3,4,5,6,7,8,9
BA479 4,7,9,12,15
BA483 7,9,12,18
I tried to use a coalesce statement like the following, but I can't get it to work because of the goofy syntax for coalesce. Is there a better way to accomplish this goal or am I just making a dumb syntactical error?
declare @StudentsID_CSVString varchar(128)
select Dept + Level as 'Class',
coalesce(@StudentsID_CSVString + ',', '')
+ CAST(StudentID as varchar(8)) as 'StudentsID_CSVString'
from Classes
left outer join StudentsInClasses ON Classes.ID = StudentsInClasses.ClassID
I used the following code to test:
declare @Students table (ID int, Name varchar(50), Gpa decimal(3,2))
declare @Classes table (ID int, Dept varchar(4), [Level] varchar(3))
declare @StudentsInClasses table (StudentID int, ClassID int)
declare @StudentsID_CSVString varchar(128)
insert into @Students (ID) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18)
insert into @Classes (ID, Dept, [Level]) values (1, 'BA', '302'), (2, 'BA', '379'), (3, 'BA', '483')
insert into @StudentsInClasses (StudentID, ClassID) values
(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),(9,1),
(4,2),(7,2),(9,2),(12,2),(15,2),
(7,3),(9,3),(12,3),(18,3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用逗号分隔字符串填充变量
@StudentsID_CSVString
的技术,您一次只能获取一个班级的字符串。要在结果集中获取多个类,您可以使用for xml path
。在此处测试查询: https://data.stackexchange.com/stackoverflow/q/115573/
With the technique to fill a variable
@StudentsID_CSVString
with a comma separated string you will only be able to get the string for one class at a time. To get more than one class in the result set you can usefor xml path
.Test the query here: https://data.stackexchange.com/stackoverflow/q/115573/