SQL SERVER中的XML PATH如何NEST

时间:2014-06-25 11:48:11

标签: sql-server xml

我是使用XML的新手,我认为这是一个简单的问题,但我无法正确使用格式。

我试图获得此输出

<Contracts>
  <Contract>
    <Ref>P000006-140</Ref>
    <ReportingTransactionAmountEntry>
      <TechAccountAmtItem>
        <Amt Ccy="GBP">7500.0000</Amt>
      </TechAccountAmtItem>
    </ReportingTransactionAmountEntry>
  </Contract>
  <Contract>
    <Ref>P000006-140</Ref>
    <ReportingTransactionAmountEntry>
      <TechAccountAmtItem>
       <Amt Ccy="GBP">100.0000</Amt>
      </TechAccountAmtItem>
      <TechAccountAmtItem>
        <Amt Ccy="GBP">7600.0000</Amt>
      </TechAccountAmtItem>
    </ReportingTransactionAmountEntry>
  </Contract>
  <Contract>
    <Ref>P000006-140</Ref>
    <ReportingTransactionAmountEntry>
      <TechAccountAmtItem>
        <Amt Ccy="EUR">500.0000</Amt>
      </TechAccountAmtItem>
    </ReportingTransactionAmountEntry>
  </Contract>
</Contracts>

看起来像这样, 联系人参考仅在较高级别显示一次

<Contracts>
  <Contract>
    <Ref>P000006-140</Ref>
        <ReportingTransactionAmountEntry>
          <TechAccountAmtItem>
            <Amt Ccy="GBP">7500.0000</Amt>
          </TechAccountAmtItem>
        </ReportingTransactionAmountEntry>
        <ReportingTransactionAmountEntry>
          <TechAccountAmtItem>
            <Amt Ccy="GBP">100.0000</Amt>
          </TechAccountAmtItem>
          <TechAccountAmtItem>
            <Amt Ccy="GBP">7600.0000</Amt>
          </TechAccountAmtItem>
        </ReportingTransactionAmountEntry>
        <ReportingTransactionAmountEntry>
          <TechAccountAmtItem>
            <Amt Ccy="EUR">500.0000</Amt>
          </TechAccountAmtItem>
        </ReportingTransactionAmountEntry>
  </Contract>
</Contracts>

我的查询看起来像这样

    SELECT 
        RTRIM(PolicyRef) AS  'Ref', 
            (SELECT * FROM
                (
                    select
                    RTRIM(SettlementCurrency) AS 'TechAccountAmtItem/Amt/@Ccy',
                    Paid_This_Time_Indemnity AS 'TechAccountAmtItem/Amt'
                    UNION
                    SELECT
                    RTRIM(SettlementCurrency) AS 'TechAccountAmtItem/Amt/@Ccy',
                    Paid_To_Date_Indemnity AS 'TechAccountAmtItem/Amt'
                ) iq FOR XML PATH(''),TYPE
             ) AS 'ReportingTransactionAmountEntry' 
    FROM #tmpClm3   
    FOR XML PATH ('Contract'),ROOT('Contracts')   

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要在主查询中对PolicyRef进行分组,并在子查询中获取详细信息。

select T1.PolicyRef as [Ref],
       (
       select 
         (
         select T2.SettlementCurrency as [TechAccountAmtItem/Amt/@Ccy],
                T2.Paid_This_Time_Indemnity as [TechAccountAmtItem/Amt]
         where T2.Paid_This_Time_Indemnity is not null
         for xml path(''), type
         ),
         (
         select T2.SettlementCurrency as [TechAccountAmtItem/Amt/@Ccy],
                T2.Paid_To_Date_Indemnity as [TechAccountAmtItem/Amt]
         where T2.Paid_To_Date_Indemnity is not null
         for xml path(''), type
         )
       from T as T2
       where T1.PolicyRef = T2.PolicyRef
       for xml path('ReportingTransactionAmountEntry'), type
       )
from T as T1
group by T1.PolicyRef
for xml path('Contract'), root('Contracts')

SQL Fiddle