SQL Server - 从多个级别

时间:2017-11-16 00:24:55

标签: sql-server xml

我想要将图表名称的XML层次结构值选择到该图表下所有过滤器值的第一列。

我使用了下面的方法,但ChartName返回NULL。我怀疑我需要做一个子查询

DECLARE @input XML = 
'<Report>
    <DataSets>
    </DataSets>
    <ReportSections>
      <ReportSection>
        <ReportItems>
          <Chart Name="Hub1">
            <Filters>
                <Filter>
                  <Expression>Fields!Hub.Value</Expression>
                  <Operator>Like</Operator>
                  <Values>
                    <Value>Central</Value>
                  </Values>
                </Filter>
                <Filter>
                    <Expression>Fields!ADP_Hrs.Value</Expression>
                    <Operator>NotEqual</Operator>
                    <Values>
                      <Value DataType="Float">0</Value>
                    </Values>
                </Filter>
                <Filter>
                    <Expression>Fields!TL.Value</Expression>
                    <Operator>Equal</Operator>
                    <Values>
                      <Value DataType="Integer">1</Value>
                    </Values>
                </Filter>
            </Filters>
          </Chart>  
        </ReportItems>
      </ReportSection>
    </ReportSections>
</Report>'

SELECT
    [Tbl].[Col].value('Chart [4]', 'varchar(50)') as ChartName,
    [Tbl].[Col].value('Expression [1]', 'varchar(50)') as Expression,
    [Tbl].[Col].value(' Operator [1]', 'varchar(50)') as 'Operator',
    [Tbl].[Col].value(' Values [1]', 'varchar(50)') as 'Value'
FROM 
    @input.nodes('Report/ReportSections/ReportSection/ReportItems/Chart/Filters/Filter') as [Tbl]([Col])

预期产出:

ChartName  Expression              Operator    Value
------------------------------------------------------
Hub1       Fields!Hub.Value        Like        Central
Hub1       Fields!ADP_Hrs.Value    NotEqual    0 
Hub1       Fields!TL.Value         Equal       1

2 个答案:

答案 0 :(得分:1)

Chart不在Filter元素中,因此它无法找到它,除非您向后走XML路径,然后拉{的@Name属性{1}}元素:

Chart

返回:

SELECT
[Tbl].[Col].value('(../../@Name)[1]', 'varchar(50)') as ChartName,
[Tbl].[Col].value('Expression [1]', 'varchar(50)') as Expression,
[Tbl].[Col].value(' Operator [1]', 'varchar(50)') as 'Operator',
[Tbl].[Col].value(' Values [1]', 'varchar(50)') as 'Value'

FROM @input.nodes('Report/ReportSections/ReportSection/ReportItems/Chart/Filters/Filter') as [Tbl]([Col])

答案 1 :(得分:1)

又一个选择

示例

Select ChartName    = lvl1.n.value('@Name'        ,'varchar(50)') 
      ,Expression   = lvl2.n.value('Expression[1]','varchar(50)') 
      ,Operator     = lvl2.n.value('Operator[1]'  ,'varchar(50)') 
      ,Value        = lvl2.n.value('Values[1]'    ,'varchar(50)') 
 From  @input.nodes('Report/ReportSections/ReportSection/ReportItems/*') lvl1(n)
 Cross Apply lvl1.n.nodes('Filters/Filter') lvl2(n)

<强>返回

ChartName   Expression              Operator    Value
Hub1        Fields!Hub.Value        Like        Central
Hub1        Fields!ADP_Hrs.Value    NotEqual    0
Hub1        Fields!TL.Value         Equal       1