【问题标题】:SQL Server: Two-level GROUP BY with XML outputSQL Server:具有 XML 输出的两级 GROUP BY
【发布时间】:2013-08-08 17:14:26
【问题描述】:

我有一个分层数据表,我试图将其选择为单个分组的 XML 值:

列:Id, Type, SubType, SubSubType

样本数据:

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

对于以上数据,我想输出如下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>

即包含上表中所有行的单个 xml 结构,按第一列 (Type) 分组,并按第二列 (SubType) 进一步分组。

【问题讨论】:

  • 欢迎来到stackoverflow。描述得很好!。请分享您迄今为止尝试过的示例代码,以便其他用户可以轻松帮助您。

标签: sql sql-server-2008 sqlxml


【解决方案1】:
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')

【讨论】:

  • 叮叮叮!那成功了。我不得不 CAST TO XML 两个内部选择,但结果是完美的。谢谢!
  • @user1822597 你不应该需要演员表。你忘了,type吗?
  • @user1822597 您是否尝试使用答案中的代码复制粘贴?也许您忘记了type 之前的逗号?
  • 我的错——我在这篇文章中使用的列名是虚构的,“类型”是一个糟糕的选择。您的解决方案按原样完美运行。再次感谢!
【解决方案2】:
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

【讨论】:

  • 我也试过这个,但由于 GROUP BY,接受的答案给出了更好的 xml 结构。
猜你喜欢
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 2011-12-10
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多