返回重复列的嵌套XML

时间:2011-05-24 00:03:55

标签: sql sql-server xml sql-server-2008

我有一个看起来像这样的表,适当地放置代码(特别是XML结构和查询)

Class Subclass   Information  Details
Classx subclassx Info1        otherinfo1 
Classx subclassx Info2
Classx subclassy info3 
Classyy subclassyyy info3 

举个例子,我需要返回用Class ='classx'过滤的行,返回:

<Class Name="Classx">
  <Subclass subclassName ="subclassx">
    <Info>Info1</Info>
    <Info>Info2</Info>
  </Subclass>  
 <Subclass subclassName ="subclassy">
    <Info>Info3</Info>
  </Subclass>  
</Class>

(或)

<Class Name="Classx">
  <Subclass>subclassx
    <Info>Info1</Info>
    <Info>Info2</Info>
  </Subclass>
 <Subclass>subclassy
   <Info>Info3</Info>
 </Subclass>
</Class>

在'Class'元素中嵌入'Subclass'的东西,在'Subclass'元素中嵌入'Info'。

尝试了一些查询:

DECLARE @classFilter nvarchar(15);
SELECT @classFilter = 'Classx' 

     SELECT @classFilter as '@Class',
              (SELECT  r1.subClass as 'SubClass',
               (SELECT  DISTINCT r1.Info as 'Info' 
                   FROM @results as r2 
                   WHERE r2.Class=  @classFilter and r1.Subclass = r2.Subclass
                    FOR XML PATH('Information'), TYPE
                    )
               FROM @results r1 
              GROUP BY r1.Class, r1.SubClass, r1.Info
              HAVING r1.Class= @classFilter
                FOR XML PATH('Subclasses'), TYPE)
           FOR XML PATH('Class'), TYPE;

给出:

<Class Class="Classx">
  <Subclasses>
    <SubClass>subclassx</SubClass>
    <Information>
      <Info>Info1</Info>
    </Information>  
  </Subclasses>
 <Subclasses>
    <SubClass>subclassx</SubClass>
    <Information>
      <Info>Info2</Info>
    </Details>  
  </Subclasses>
<Subclasses>
    <SubClass>subclassy</SubClass>
    <Information>
      <Info>Info3</Info>
    </Information>  
  </Subclasses>
</Class>

我能来的最近的是

 SELECT @classfilter as '@Name',    --or r1.Class
        (SELECT r1.Subclass as '@Subclass', 
            (SELECT DISTINCT r1.Information as 'Information' 
               FROM @results as r2 
               WHERE r2.Class =  @classFilter and r1.Subclass = r2.Subclass         
                )
           FROM @results r1           
          GROUP BY r1.Class, r1.Subclass, r1.Information
          HAVING r1.Class= @classFilter 
            FOR XML PATH('Subclasses'), TYPE)
       FOR XML PATH('Class'), TYPE;

给出:

 <Class Name="classx">
      <Subclasses subclass="subclassx">Info1</Subclasses>
      <Subclasses subclass="subclassx">Info2</Subclasses>
      <Subclasses subclass="subclassy">Info3</Subclasses>
    </Class>

想知道我错过了什么!

1 个答案:

答案 0 :(得分:0)

declare @T table
(
  Class varchar(10),
  Subclass varchar(10),
  Information varchar(10),
  Details varchar(10)
)

insert into @T 
select 'Classx', 'subclassx', 'Info1', 'otherinfo1' union all 
select 'Classx', 'subclassx', 'Info2', '' union all
select 'Classx', 'subclassy', 'info3', ''  union all
select 'Classyy', 'subclassyy', 'info3', '' 

declare @class varchar(10)
set @class = 'Classx'

select 
  T.Class as '@Name',
  (select
     T2.Subclass as '@subclassName',
     (select
        T3.Information as 'Info'
      from @T as T3
      where T3.Subclass = T2.Subclass and
            T3.Class = T.Class
      for xml path(''), type)
   from @T as T2
   where T.Class = T2.Class
   group by T2.Subclass
   for xml path('Subclass'), type)
from @T as T
where T.Class = @class
group by T.Class
for xml path('Class')