SQL Server: Two-level GROUP BY with XML output
Asked Answered
W

2

7

I have a table of hierarchical data that I am trying to select as a single, grouped XML value:

Columns: Id, Type, SubType, SubSubType

Sample data:

Id  Type                    Subtype                    SubSubType
1   Product Documentation   Brochures                  Functional Brochures
2   Product Documentation   Brochures                  Fliers
3   Product Documentation   Data Sheets and Catalogs   Data Sheets
4   Product Documentation   Data Sheets and Catalogs   Catalogs
5   Other Documentation     Other classification       User Guides

For the above data, I would like to output the following xml:

<AllTypes>
    <Type name="Product Documentation">
        <SubType name="Brochures">
            <SubSubType name="Functional Brochures"/>
            <SubSubType name="Fliers"/>
        </SubType>
        <SubType name="Data Sheets and Catalogs">
            <SubSubType name="Data Sheets"/>
            <SubSubType name="Catalogs"/>
        </SubType>
    </Type>
    <Type name="Other Documentation">
        <SubType name="Other classification">
            <SubSubType name="User Guides"/>
        </SubType>
    </Type>
</AllTypes>

i.e. a single xml structure containing all rows from the above table, grouped by the first column (Type), and further grouped by the second column (SubType).

Warnke answered 8/8, 2013 at 17:14 Comment(1)
Welcome to stackoverflow. Well described!. Please share the sample code that you have tried so far so that other users can help you out easily.Kramatorsk
C
17
declare @T table
(
  ID int,
  Type varchar(30),
  SubType varchar(30),
  SubSubType varchar(30)
)

insert into @T values
(1, 'Product Documentation', 'Brochures',                'Functional Brochures'),
(2, 'Product Documentation', 'Brochures',                'Fliers'),
(3, 'Product Documentation', 'Data Sheets and Catalogs', 'Data Sheets'),
(4, 'Product Documentation', 'Data Sheets and Catalogs', 'Catalogs'),
(5, 'Other Documentation',   'Other classification',     'User Guides')

select T1.Type as '@Name',
       (
       select T2.SubType as '@Name',
              (
              select T3.SubSubType as '@Name'
              from @T as T3
              where T3.SubType = T2.SubType and
                    T3.Type = T1.Type
              for xml path('SubSubType'), type
              )
       from @T as T2
       where T2.Type = T1.Type
       group by T2.SubType
       for xml path('SubType'), type
       )
from @T as T1
group by Type
for xml path('Type'), root('AllTypes')
Counterforce answered 8/8, 2013 at 17:32 Comment(4)
ding ding ding! That did the trick. I had to CAST TO XML the two inner selects, but the results are perfect. Thanks!Warnke
@user1822597 You should not need the cast. Did you forget ,type?Counterforce
@user1822597 Did you try a copy paste with the code in the answer? Perhaps you forgot the comma before type?Counterforce
My bad -- the column names I used in this writeup were fictional, and "Type" was a bad choice. Your solution works perfectly as-is. Thanks again!Warnke
E
4
select
   T1.Type as name,
   (
     select
        T2.SubType as name,
        (
          select T3.SubSubType as name
          from Table1 as T3
          where T3.Type = T1.Type and T3.SubType = T2.SubType
          for xml raw('SubSubType'), type
        )
     from (select distinct Type, SubType  from Table1) as T2
     where T1.Type = T2.Type
     for xml raw('SubType'), type
   )
from (select distinct Type from Table1) as T1
for xml raw('Type'), root('AllTypes')

sql fiddle demo

Emrich answered 8/8, 2013 at 17:34 Comment(1)
I tried this, too, but the accepted answer gives nicer xml structure because of the GROUP BYs.Irenics

© 2022 - 2024 — McMap. All rights reserved.