XML显式 - 无法获得层次结构

时间:2018-04-25 18:04:06

标签: sql xml

我有一张下表,其内容如下:

productType product comments    val
LL          P1       T1       5801309.42
LL          P2       T2       336320.76
LL          P2       T2       311734.56
LL          P2       T2       136381.42
LL          P2       T2       91023.48

我使用以下查询使用XML Explicit和3级别的heirarchy 级别1 - >级别2 - >级别3

SELECT distinct 1    as Tag,  
       NULL as Parent,  
       productType as [Level1!1!productType],  
       NULL       as [Level2!2!product]    ,
       NULL       as [Level3!3!comments!ELEMENT]   ,
       NULL       as [Level3!3!val!ELEMENT]
FROM   #t1  
UNION ALL  
SELECT distinct 2 as Tag,  
       1 as Parent,  
       productType,  
       product ,
        NULL,
        NULL 
FROM   #t1  
UNION ALL  
SELECT distinct 3 as Tag,  
       2 as Parent,  
       productType,  
       product ,
        comments,val 
FROM   #t1 
ORDER BY 1,2,3,4
FOR XML EXPLICIT; 

我得到以下XML输出。 2级产品类型P1未获得3级

<Level1 productType="LL">
  <Level2 product="P1" />
  <Level2 product="P2">
    <Level3>
      <comments>T1</comments>
      <val>5801309.42</val>
    </Level3>
    <Level3>
      <comments>T2</comments>
      <val>91023.48</val>
    </Level3>
    <Level3>
      <comments>T2</comments>
      <val>136381.42</val>
    </Level3>
    <Level3>
      <comments>T2</comments>
      <val>311734.56</val>
    </Level3>
    <Level3>
      <comments>T2</comments>
      <val>336320.76</val>
    </Level3>
  </Level2>
</Level1>

但是期望的输出是

<Level1 productType="LL">
  <Level2 product="P1" >
    <Level3>
        <comments>T1</comments>
        <val>5801309.42</val>
    </Level3>
  </Level2>
  <Level2 product="P2">
    <Level3>
      <comments>T2</comments>
      <val>91023.48</val>
    </Level3>
    <Level3>
      <comments>T2</comments>
      <val>136381.42</val>
    </Level3>
    <Level3>
      <comments>T2</comments>
      <val>311734.56</val>
    </Level3>
    <Level3>
      <comments>T2</comments>
      <val>336320.76</val>
    </Level3>
  </Level2>
</Level1>

代码中是否有任何内容 - 使用3个union语句缺失指令将Lelvel3与Level 2相关联

----通过-------

确定订单后查询
SELECT distinct 1    as Tag,  
       NULL as Parent,  
       productType as [Level1!1!productType],  
       NULL       as [Level2!2!product]    ,
       NULL       as [Level3!3!comments!ELEMENT]   ,
       NULL       as [Level3!3!val!ELEMENT]
FROM   #t1  
UNION ALL  
SELECT distinct 2 as Tag,  
       1 as Parent,  
       productType,  
       product ,
        NULL,
        NULL 
FROM   #t1  
UNION ALL  
SELECT distinct 3 as Tag,  
       2 as Parent,  
       productType,  
       product ,
        comments,val 
FROM   #t1 
ORDER BY  4,3,2,1
FOR XML EXPLICIT; 

1 个答案:

答案 0 :(得分:0)

行的顺序很重要。
参考Here
您的查询输出为:

TAG PARENT  LEVEL1  LEVEL2  LEVEL3  LEVEL3ELEMENT
1   (null)  LL      (null)  (null)  (null)
2   1       LL      P1      (null)  (null)
2   1       LL      P2      (null)  (null)
3   2       LL      P1      T1      5801309
3   2       LL      P2      T2      136381
3   2       LL      P2      T2      311735
3   2       LL      P2      T2      336321
3   2       LL      P2      T2      91023

对于您的结果,它需要:

TAG PARENT  LEVEL1  LEVEL2  LEVEL3  LEVEL3ELEMENT
1   (null)  LL      (null)  (null)  (null)
2   1       LL      P1      (null)  (null)
3   2       LL      P1      T1      5801309
2   1       LL      P2      (null)  (null)
3   2       LL      P2      T2      136381
3   2       LL      P2      T2      311735
3   2       LL      P2      T2      336321
3   2       LL      P2      T2      91023

第3行和第4行应交换为元素T1以置于元素P1

试试这个:

SELECT * FROM
(SELECT distinct 1    as Tag,  
       NULL as Parent,  
       productType as Level1,  
       NULL       as Level2    ,
       NULL       as Level3   ,
       NULL       as Level3ELEMENT
FROM   t1  
UNION ALL  
SELECT distinct 2 as Tag,  
       1 as Parent,  
       productType,  
       product ,
        NULL,
        NULL 
FROM   t1  
UNION ALL  
SELECT distinct 3 as Tag,  
       2 as Parent,  
       productType,  
       product ,
        comments,val 
FROM   t1 
) asd
order by 
CASE WHEN Level1 is null THEN '1'
ELSE Level1 End,
CASE WHEN Level2 is null THEN '1'
ELSE Level2 End,
CASE WHEN Level3 is null THEN '1'
ELSE Level3 End