sql >> データベース >  >> RDS >> Sqlserver

SQL Server:XML 出力を使用した 2 レベルの GROUP BY

    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. PostgreSQL:コマンドラインからパラメータを渡す方法は?

    2. SQL ServerのCONVERT()で使用可能な日付形式のリスト

    3. Oracleでの文字セットの不一致エラー

    4. 一意の日時範囲のPostgres制約