我得到了两个数据表,如下所示:
表 1:学生
表 2:主题
我需要输出为:
我通过使用 XML PATH 的以下查询实现了这一点
代码:
WITH cte
AS ( SELECT Stu.Student_Id ,
Stu.Student_Name ,
( SELECT Sub.[Subject] + ','
FROM [Subject] AS Sub
WHERE Sub.Student_Id = Stu.Student_Id
ORDER BY Sub.[Subject]
FOR
XML PATH('')
) AS [Subjects]
FROM dbo.Student AS Stu
)
SELECT Student_id [Student Id] ,
student_name [Student Name] ,
SUBSTRING(Subjects, 1, ( LEN(Subjects) - 1 )) AS [Student Subjects]
FROM cte
我的问题是不使用 XML Path 有更好的方法吗?