我有一个我创建的查询,它输出项目以及谁在展示项目。我的问题是我试图将演示者分组到项目ID,因此它列出了项目ID下的演示者,而不是多次包括它。
这是我的问题:
DECLARE @agendaID AS INT = '23';
SELECT
(SELECT att.empID,
emp.firstName,
emp.lastName,
emp.ntid
FROM dbo.BS_ProjectReview_Attendees AS att
LEFT OUTER JOIN
dbo.EmployeeTable AS emp
ON att.empID = emp.EmpID
WHERE att.agendaID = @agendaID
FOR XML PATH ('attendee'), TYPE, ELEMENTS, ROOT ('attendees')),
(SELECT pres.intID,
int.intTitle,
(SELECT emp.firstName,
emp.lastName,
emp.ntid
FROM dbo.EmployeeTable AS emp
WHERE emp.EmpID = pres.empID
FOR XML PATH ('presenter'), TYPE, ELEMENTS, ROOT ('presenters'))
FROM dbo.BS_ProjectReview_ProjectPresenters AS pres
INNER JOIN dbo.BS_Initiatives AS int
ON pres.intID = int.intID
WHERE pres.agendaID = @agendaID
GROUP BY int.intID <----- ISSUE IS HERE
FOR XML PATH ('project'), TYPE, ELEMENTS, ROOT ('projects'))
FOR XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');
这是我的XML OUTPUT:
<root>
<data>
<attendees>
<attendee>
<empID>1234</empID>
<firstName>Mike</firstName>
<lastName>Smith</lastName>
<ntid>msmith</ntid>
</attendee>
</attendees>
<projects>
<project>
<intID>202</intID>
<intTitle>Infrastructure Expansion</intTitle>
<presenters>
<presenter>
<firstName>Bob</firstName>
<lastName>Jones</lastName>
<ntid>bjones</ntid>
</presenter>
</presenters>
</project>
<project>
<intID>202</intID>
<intTitle>Infrastructure Expansion</intTitle>
<presenters>
<presenter>
<firstName>User 1</firstName>
<lastName>Last 1</lastName>
<ntid>ulast1</ntid>
</presenter>
</presenters>
</project>
</projects>
</data>
</root>
这是所需的输出:
<root>
<data>
<attendees>
<attendee>
<empID>1234</empID>
<firstName>Mike</firstName>
<lastName>Smith</lastName>
<ntid>msmith</ntid>
</attendee>
</attendees>
<projects>
<project>
<intID>202</intID>
<intTitle>Infrastructure Expansion</intTitle>
<presenters>
<presenter>
<firstName>Bob</firstName>
<lastName>Jones</lastName>
<ntid>bjones</ntid>
</presenter>
<presenter>
<firstName>User 1</firstName>
<lastName>Last 1</lastName>
<ntid>ulast1</ntid>
</presenter>
</presenters>
</project>
</projects>
</data>
</root>
它应该在同一个intID
下列出两个演示者,但它们会复制它们。
我收到group by子句的错误,因为它没有包含在聚合函数中,并希望我包含pres.empID
但是它不会使它独特,因为它们都是不同的。
有什么想法吗?
答案 0 :(得分:1)
这可以用这个解决,但是,因为SQLFiddle是ATM,没有例子。
DECLARE @agendaID AS INT = '23';
SELECT
(SELECT att.empID,
emp.firstName,
emp.lastName,
emp.ntid
FROM dbo.BS_ProjectReview_Attendees AS att
LEFT OUTER JOIN
dbo.EmployeeTable AS emp
ON att.empID = emp.EmpID
WHERE att.agendaID = @agendaID
FOR XML PATH ('attendee'), TYPE, ELEMENTS, ROOT ('attendees')),
(SELECT pres.intID,
int.intTitle,
(SELECT emp.firstName,
emp.lastName,
emp.ntid
FROM dbo.EmployeeTable AS emp
INNER JOIN dbo.BS_ProjectReview_ProjectPresenters AS pres ON emp.EmpID = pres.empID
WHERE pres.intID = int.intID
FOR XML PATH ('presenter'), TYPE, ELEMENTS, ROOT ('presenters'))
FROM dbo.BS_Initiatives AS int
WHERE @agendaID IN ( SELECT pres.agendaID FROM dbo.BS_ProjectReview_ProjectPresenters AS pres WHERE pres.intID = int.intID)
FOR XML PATH ('project'), TYPE, ELEMENTS, ROOT ('projects'))
FOR XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');