2013-08-08 19 views
5

Ho una tabella di dati gerarchici che sto cercando di selezionare come un singolo, il valore XML raggruppati:SQL Server: a due livelli GROUP BY con output XML

Colonne: dati Id, Type, SubType, SubSubType

Esempio:

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 

per i dati di cui sopra, desidero uscita seguente 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> 

cioè una singola struttura xml contenente tutte le righe dalla tabella precedente, raggruppate per la prima colonna (Tipo) e ulteriormente raggruppate dalla seconda colonna (Sottotipo).

+0

Benvenuti a 'stackoverflow'. Ben descritto !. Si prega di condividere il codice di esempio che avete provato fino ad ora in modo che altri utenti possano aiutarvi facilmente. –

risposta

9
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') 
+0

ding ding ding! Questo ha fatto il trucco. Ho dovuto CAST TO XML i due selezioni interiori, ma i risultati sono perfetti. Grazie! – gice

+0

@ user1822597 Non è necessario il cast. Hai dimenticato ', scrivi'? –

+0

Per qualche motivo, digitare mi ha dato un errore di sintassi? – gice

1
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

+0

Ho provato anche questo, ma la risposta accettata fornisce una struttura xml migliore a causa dei BY GROUP. – mkataja