Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

SQL Server:GROUP BY dua tingkat dengan keluaran XML

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')


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara mendapatkan koordinat pembatas untuk kode pos (zip) AS?

  2. Kepatuhan GDPR dan SQL Server Anda

  3. Kembalikan Nama Workstation Saat Ini yang Terhubung ke SQL Server (T-SQL)

  4. Tidak dapat membuat instance penyedia OLE DB Microsoft.Jet.OLEDB.4.0 untuk server tertaut null

  5. Klausa Check-in Bersyarat di mana