我有三张桌子。
我现在有三个查询,
根据GroupID
查询从Extention_Group
表中获取不同的项目
select distinct GroupID from Extention_Group order by GroupID
查询从表中获取所需的必需列
select g.ID as GroupID, g.Name as GroupName, eg.ExtentionID from [Group] g
inner join Extention_Group eg on eg.GroupId = g.Id
inner join UserProfile up on eg.ExtentionID = up.UserProfileID
并将查询扩展为CSV(逗号分隔值)
SELECT SUBSTRING(
(SELECT ',' + up.Extension
FROM userprofile up
ORDER BY up.Extension
FOR XML PATH('')),2,200000) AS CsvExtension
我真正想要的是将所有这些结果合并到一个表中。 我如何将这三个结果合并到一个表中,以便我可以轻松使用它。 我已经尝试过UNIONS但没有成功。
答案 0 :(得分:0)
它可以像这样组合,
select g.ID as GroupID, g.Name as GroupName, up.Extension, eg.ExtentionID
,stuff((SELECT ',' + up.Extension FROM userprofile up1
FOR XML PATH('')),1,1,'') AS CsvExtension
,EG1.Groupid as [distinct Groupid]
from [Group] g
inner join Extention_Group eg on eg.GroupId = g.Id
inner join UserProfile up on eg.ExtentionID = up.UserProfileID
inner join (select distinct GroupID from Extention_Group ) EG1 on eg.groupid=eg1.groupid
知道确切的关系和数据可以帮助优化它。我不知道它是如何表现的。可能你应该从“xml path select”中删除where子句