T SQL For XML PATH Group By as Attribute or Element
Asked Answered
R

2

6

I have been working on the T-SQL FOR XML with PATH Mode to create a Hierarchy based on the group by field. Below is my query and output. Pls help me with your valuable suggestions. Thank you. Good Day!!!

select e.department_id AS [@DepartmentID],
d.DEPARTMENT_NAME AS [@DepartmentName],
e.EMPLOYEE_ID AS [EmployeeInfo/EmployeeID],
e.FIRST_NAME AS [EmployeeInfo/FirstName],
e.LAST_NAME AS [EmployeeInfo/LastName]
from employees e
JOIN departments d 
ON e.department_id = d.department_id
GROUP BY e.department_id,d.DEPARTMENT_NAME,
e.EMPLOYEE_ID,e.FIRST_NAME,e.LAST_NAME
FOR XML PATH ('Department'), ROOT ('Departments')

Output:

 <Departments>
  <Department DepartmentID="10">
    <EmployeeInfo>
      <EmployeeID>111</EmployeeID>
      <FirstName>John</FirstName>
      <LastName>Chen</LastName>
    </EmployeeInfo>
  </Department>
  <Department DepartmentID="10">
    <EmployeeInfo>
      <EmployeeID>201</EmployeeID>
      <FirstName>steven</FirstName>
      <LastName>Whalen</LastName>
    </EmployeeInfo>
  </Department>
  <Department DepartmentID="30">
    <EmployeeInfo>
      <EmployeeID>105</EmployeeID>
      <FirstName>ANIRUDH</FirstName>
      <LastName>RAMESH</LastName>
    </EmployeeInfo>
  </Department>
  <Department DepartmentID="30">
    <EmployeeInfo>
      <EmployeeID>115</EmployeeID>
      <FirstName>Den</FirstName>
      <LastName>Raphaely</LastName>
    </EmployeeInfo>
  </Department>
<Departments>

Desired Output is :

<Departments>
  <Department DepartmentID="10">
    <EmployeeInfo>
      <EmployeeID>111</EmployeeID>
      <FirstName>John</FirstName>
      <LastName>Chen</LastName>
    </EmployeeInfo>
    <EmployeeInfo>
      <EmployeeID>201</EmployeeID>
      <FirstName>steven</FirstName>
      <LastName>Whalen</LastName>
    </EmployeeInfo>
  </Department>
  <Department DepartmentID="30">
    <EmployeeInfo>
      <EmployeeID>105</EmployeeID>
      <FirstName>ANIRUDH</FirstName>
      <LastName>RAMESH</LastName>
    </EmployeeInfo>
    <EmployeeInfo>
      <EmployeeID>115</EmployeeID>
      <FirstName>Den</FirstName>
      <LastName>Raphaely</LastName>
    </EmployeeInfo>
  </Department>
<Departments>
Romola answered 25/4, 2017 at 7:18 Comment(0)
S
5

You could use TYPE for nested xml

SELECT 
      d.department_id AS [@DepartmentID],
      d.DEPARTMENT_NAME AS [@DepartmentName], 
      (
         SELECT 
                e.EMPLOYEE_ID AS EmployeeID,
                e.FIRST_NAME AS [FirstName],
                e.LAST_NAME AS [LastName]  
         FROM employees e
         WHERE e.department_id = d.department_id
         FOR XML PATH ('EmployeeInfo'), TYPE
      )
FROM departments d 
FOR XML PATH ('Department'), ROOT ('Departments')
Saxon answered 25/4, 2017 at 8:45 Comment(1)
Thank You :D. Now I am able to get the desired output.Romola
R
1

Not sure, whether we can answer our own question. Me & one of my colleague has found another solution for this query but with AUTO Mode.

select d.DEPARTMENT_ID as [DepartmentID],e.EMPLOYEE_ID as 
[EmployeeID],e.first_name as [EmployeeName],e.SALARY as [Salary]
from [departments] d
inner join [employees] e
on e.DEPARTMENT_ID = d.DEPARTMENT_ID
order by 1,4
for xml AUTO, Root ('Employees'), ELEMENTS
Romola answered 26/4, 2017 at 3:37 Comment(1)
Why was this downvoted? I had a performance issue with the subquery approach and so followed the advice of this answer and it worked for me.Fondue

© 2022 - 2024 — McMap. All rights reserved.