2015-01-08 76 views
0

我有一個查詢,我創建了哪個輸出項目以及誰在呈現項目。我的問題是,我試圖將演示者分組到項目ID,以便列出項目ID下的演示者並且不多次包含它。TSQL組按問題導致重複

這裏是我的查詢:

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輸出:

<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

很確定你需要@語法。見http://stackoverflow.com/questions/8483936/tsql-for-xml-explicit – Paparazzi

+0

這篇文章似乎並沒有涉及到我的問題。問題在於我在問題中確定的小組小句。 – SBB

+0

爲什麼不發佈期望的輸出 – Paparazzi

回答

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');