Question

I have a table in MSSQL 2008 which have data like

  row1 ='<FullData>
    <Employees>
          <Employee ID="001" Name="David" />
          <Employee ID="002" Name="Mike" />
          <Employee ID="003" Name="Alex" />
          <Employee ID="004" Name="Morris" />
    </Employees>
    <Departments>
            <Department ID="01" Name="Food"/>
    </Departments>
    <Groups>
    </Groups>
</FullData>'
row2 =  '<FullData>
    <Employees>
          <Employee ID="005" Name="Fox" />
          <Employee ID="006" Name="Perry" />
          <Employee ID="007" Name="Duals" />
          <Employee ID="008" Name="Harry" />
    </Employees>
    <Departments>
            <Department ID="02" Name="Mobiles"/>
    </Departments>
    <Groups>
            <Group Name="Electronics">
    </Groups>
</FullData>'

and similar. How can i fetch data into a single table having this structure:

EmployeeID  ||  EmoloyeeName    ||  DepartmentID    ||  DepartmentName  ||  Groups
001,002,..  ||  David,Mike,...  ||  01              ||  Food            ||  
005,006,..  ||  Fox,Perry,..    ||  02              ||  Mobiles         ||  Electronics

Any suggestions??

Update1

currently i can get value from hard coded value not form table data of only one tag in multiple rows like

declare @x xml

set @x = '<FullData>
        <Employees>
              <Employee ID="001" Name="David" />
              <Employee ID="002" Name="Mike" />
              <Employee ID="003" Name="Alex" />
              <Employee ID="004" Name="Morris" />
        </Employees>
        <Departments>
                <Department ID="01" Name="Food"/>
        </Departments>
        <Groups>
        </Groups>
    </FullData>'

select emp.e.value('@ID','varchar(50)') as ID, 
        emp.e.value('@Name','varchar(50)') as NAME 
    from @x.nodes('FullData/Employees/Employee') as emp(e)

and Output is

001 David
002 Mike
003 Alex
004 Morris
Was it helpful?

Solution

Try below SQL:

declare @row1 xml;
set @row1 ='<FullData>
    <Employees>
          <Employee ID="001" Name="David" />
          <Employee ID="002" Name="Mike" />
          <Employee ID="003" Name="Alex" />
          <Employee ID="004" Name="Morris" />
    </Employees>
    <Departments>
            <Department ID="01" Name="Food"/>
    </Departments>
    <Groups>
    </Groups>
</FullData>';

declare @row2 xml;
set @row2 =  '<FullData>
    <Employees>
          <Employee ID="005" Name="Fox" />
          <Employee ID="006" Name="Perry" />
          <Employee ID="007" Name="Duals" />
          <Employee ID="008" Name="Harry" />
    </Employees>
    <Departments>
            <Department ID="02" Name="Mobiles"/>
    </Departments>
    <Groups>
            <Group Name="Electronics" />
    </Groups>
</FullData>'
;
with CTE as
(
    select 
     @row1.value('(/FullData/Departments/Department/@ID)[1]','nvarchar(15)') as 'DepartmentId'
     ,emp.ee.value('@ID','nvarchar(5)') as 'EmployeeID'
     ,emp.ee.value('@Name','nvarchar(5)') as 'EmployeeName'
     ,@row1.value('(/FullData/Departments/Department/@Name)[1]','nvarchar(15)') as 'DepartmentName'
     ,@row1.value('(/FullData/Groups/Group/@Name)[1]','nvarchar(15)') as 'Groups'
    from
        @row1.nodes('(/FullData/Employees/Employee)') as emp(ee)
    union all
    select 
     @row2.value('(/FullData/Departments/Department/@ID)[1]','nvarchar(15)') as 'DepartmentId'
     ,emp.ee.value('@ID','nvarchar(5)') as 'EmployeeID'
     ,emp.ee.value('@Name','nvarchar(5)') as 'EmployeeName'
     ,@row2.value('(/FullData/Departments/Department/@Name)[1]','nvarchar(15)') as 'DepartmentName'
     ,@row2.value('(/FullData/Groups/Group/@Name)[1]','nvarchar(15)') as 'Groups'
    from
        @row2.nodes('(/FullData/Employees/Employee)') as emp(ee)
) 
select
    cast(d.EmployeeID as nvarchar(20)) as EmployeeId
    ,cast(d.EmployeeName as nvarchar(25)) as EmployeeName
    ,d.DepartmentId
    ,d.DepartmentName
    ,d.Groups
from
(
select 
    distinct Substring(stuff
    (
        (select ',' + c2.EmployeeId from cte c2 where c2.DepartmentId = c.DepartmentId for xml path(''))
        ,1,0,''
    ),2,len(stuff
                (
                    (select ',' + c2.EmployeeId from cte c2 where c2.DepartmentId = c.DepartmentId for xml path(''))
                    ,1,0,''
                )
            )
    ) as 'EmployeeID'
    , Substring(stuff
    (
        (select ',' + c2.EmployeeName from cte c2 where c2.DepartmentId = c.DepartmentId for xml path(''))
        ,1,0,''
    ),2, len(stuff
                (
                    (select ',' + c2.EmployeeName from cte c2 where c2.DepartmentId = c.DepartmentId for xml path(''))
                    ,1,0,''
                )
            )
    ) as 'EmployeeName'
    , c.DepartmentId
    , c.DepartmentName
    , c.Groups
from CTE c
) d
;

Result:

EmployeeId           EmployeeName              DepartmentId    DepartmentName  Groups
-------------------- ------------------------- --------------- --------------- ---------------
001,002,003,004      David,Mike,Alex,Morri     01              Food            NULL
005,006,007,008      Fox,Perry,Duals,Harry     02              Mobiles         Electronics

OTHER TIPS

select replace(T.XMLCol.query('data(/FullData/Employees/Employee/@ID)').value('text()[1]', 'nvarchar(max)'), ' ', ',') as EmployeeID,
       replace(T.XMLCol.query('data(/FullData/Employees/Employee/@Name)').value('text()[1]', 'nvarchar(max)'), ' ', ',') as EmployeeName,
       T.XMLCol.value('(/FullData/Departments/Department/@ID)[1]', 'nvarchar(10)') as DepartmentID,
       T.XMLCol.value('(/FullData/Departments/Department/@Name)[1]', 'nvarchar(50)') as DepartmentName,
       T.XMLCol.value('(/FullData/Groups/Group/@Name)[1]', 'nvarchar(50)') as Groups
from T

StackExchange Data Explorer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top