Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T SQL For XML PATH Group By as Attribute or Element

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>
like image 813
sriram G Avatar asked Apr 25 '17 07:04

sriram G


People also ask

What is for XML Path in SQL?

We can use FOR XML PATH to prepare a comma-separated string from the existing data. Let's create an Authors table and insert a few records into it. In the data, we can see we have an ID column and the AuthorName column. If we just select the records, it gives the output in the following format.

How do I query XML data in SQL?

SQL Server provides the XQuery feature to querying XML data type or querying with the XML column with the XPATH. Using XQuery, users can Insert, Update and Delete with the XML nodes and node values in an XML column.

Which of the following modes do you specify in a FOR XML clause?

In a FOR XML clause, you specify one of these modes: RAW. AUTO. EXPLICIT.

Can SQL be used for XML?

SQL Server lets you retrieve data as XML by supporting the FOR XML clause, which can be included as part of your query. You can use the FOR XML clause in the main (outer) query as well as in subqueries. The clause supports numerous options that let you define the format of the XML data.


2 Answers

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')
like image 166
TriV Avatar answered Oct 03 '22 19:10

TriV


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
like image 34
sriram G Avatar answered Oct 03 '22 21:10

sriram G