FOR XML PATH - 如何实现分组?

时间:2014-11-18 12:09:43

标签: sql-server xml tsql

我有一个T-SQL代码,可以从数据库表中创建XML文件。这些表包含订单响应信息。每条记录代表一条响应行。对于每个响应,有许多行。

查询为表中的许多记录返回一个结果。我想重写查询以返回每个订单响应的一个结果集(不是每一行)。 XML文件应如下所示:

<Line>
  <Line-Item>xxxxxx</Line-Item>
  <Line-Item>yyyyyy</Line-Item>
</Line>

目前代码如下:

Select '231' as "OrderResponse-Header/OrderResponseType",
   'Const' as "OrderResponse-Header/OrderResponseNumber", 
   coalesce(convert(varchar(10), GETDATE(), 20), '') as "OrderResponse-Header/OrderResponseDate",
   coalesce(T.OrderNumber, '') as "OrderResponse-Header/OrderNumber",
   coalesce(convert(varchar(10), GETDATE(), 20), '') as "OrderResponse-Header/OrderDate",
   '0' as "OrderResponse-Header/DocumentFunctionCode",

    '' as "DetailsOfTransport/TermsOfDelivery",
    '' as "OrderResponse-Parties/Buyer/ILN",
    '' as "OrderResponse-Parties/Buyer/PartyName",
    '' as "OrderResponse-Parties/Buyer/StreetAndNumber",
    '' as "OrderResponse-Parties/Buyer/CityName",
    '' as "OrderResponse-Parties/Buyer/PostCode",
    '' as "OrderResponse-Parties/Buyer/Country",

    '' as "OrderResponse-Parties/Seller/ILN",
    '' as "OrderResponse-Parties/Seller/PartyName",
    '' as "OrderResponse-Parties/Seller/StreetAndNumber",
    '' as "OrderResponse-Parties/Seller/CityName",
    '' as "OrderResponse-Parties/Seller/PostCode",
    '' as "OrderResponse-Parties/Seller/Country",

    '' as "OrderResponse-Parties/DeliveryPoint/ILN",

    '' as "OrderResponse-Parties/ShipFrom/ILN",

    coalesce(T.LineNumber, '') as "OrderResponse-Lines/Line/Line-Item/LineNumber",
    coalesce(T.BuyerItemCode, '') as "OrderResponse-Lines/Line/Line-Item/BuyerItemCode",
    '' as "OrderResponse-Lines/Line/Line-Item/ItemDescription",
    '' as "OrderResponse-Lines/Line/Line-Item/ItemStatus",
    '' as "OrderResponse-Lines/Line/Line-Item/ItemType",
    '0' as "OrderResponse-Lines/Line/Line-Item/OrderedQuantity",
    coalesce(T.QuantityToBeDelivered, '') as "OrderResponse-Lines/Line/Line-Item/QuantityToBeDelivered",
    '0' as "OrderResponse-Lines/Line/Line-Item/QuantityDifference",
    '' as "OrderResponse-Lines/Line/Line-Item/UnitOfMeasure",
    '0' as "OrderResponse-Lines/Line/Line-Item/OrderedUnitNetPrice",
    '0' as "OrderResponse-Lines/Line/Line-Item/Discount",
    coalesce(convert(varchar(10), T.ExpectedDeliveryDate, 20), '') as "OrderResponse-Lines/Line/Line-Item/ExpectedDeliveryDate",
    '0' as "OrderResponse-Summary/TotalLines"
from Import.OrderResponses as T
where T.OrderNumber = 'Gr342'
for xml path(''), root('Document-OrderResponse'), type

编辑: 以下是2个订单响应行的结果示例:Line和Line-Item节点完全分开(在2个不同的块中,但它们应该在同一个块中。

我尝试为行创建另一个表并将其连接到Order Response表,但它没有帮助。

  <Document-OrderResponse>
  <OrderResponse-Header>
    <OrderResponseType>231</OrderResponseType>
    <OrderResponseNumber>Const</OrderResponseNumber>
    <OrderResponseDate>2014-11-19</OrderResponseDate>
    <OrderNumber>Gr342</OrderNumber>
    <OrderDate>2014-11-19</OrderDate>
    <DocumentFunctionCode>0</DocumentFunctionCode>
  </OrderResponse-Header>
  <DetailsOfTransport>
    <TermsOfDelivery></TermsOfDelivery>
  </DetailsOfTransport>
  <OrderResponse-Parties>
    <Buyer>
      <ILN></ILN>
      <PartyName></PartyName>
      <StreetAndNumber></StreetAndNumber>
      <CityName></CityName>
      <PostCode></PostCode>
      <Country></Country>
    </Buyer>
    <Seller>
      <ILN></ILN>
      <PartyName></PartyName>
      <StreetAndNumber></StreetAndNumber>
      <CityName></CityName>
      <PostCode></PostCode>
      <Country></Country>
    </Seller>
    <DeliveryPoint>
      <ILN></ILN>
    </DeliveryPoint>
    <ShipFrom>
      <ILN></ILN>
    </ShipFrom>
  </OrderResponse-Parties>
  <OrderResponse-Lines>
    <Line>
      <Line-Item>
        <LineNumber>3</LineNumber>
        <BuyerItemCode>gesgrere</BuyerItemCode>
        <ItemDescription></ItemDescription>
        <ItemStatus></ItemStatus>
        <ItemType></ItemType>
        <OrderedQuantity>0</OrderedQuantity>
        <QuantityToBeDelivered>55</QuantityToBeDelivered>
        <QuantityDifference>0</QuantityDifference>
        <UnitOfMeasure></UnitOfMeasure>
        <OrderedUnitNetPrice>0</OrderedUnitNetPrice>
        <Discount>0</Discount>
        <ExpectedDeliveryDate>2014-02-12</ExpectedDeliveryDate>
      </Line-Item>
    </Line>
  </OrderResponse-Lines>
  <OrderResponse-Summary>
    <TotalLines>0</TotalLines>
  </OrderResponse-Summary>
  <OrderResponse-Header>
    <OrderResponseType>231</OrderResponseType>
    <OrderResponseNumber>Const</OrderResponseNumber>
    <OrderResponseDate>2014-11-19</OrderResponseDate>
    <OrderNumber>Gr342</OrderNumber>
    <OrderDate>2014-11-19</OrderDate>
    <DocumentFunctionCode>0</DocumentFunctionCode>
  </OrderResponse-Header>
  <DetailsOfTransport>
    <TermsOfDelivery></TermsOfDelivery>
  </DetailsOfTransport>
  <OrderResponse-Parties>
    <Buyer>
      <ILN></ILN>
      <PartyName></PartyName>
      <StreetAndNumber></StreetAndNumber>
      <CityName></CityName>
      <PostCode></PostCode>
      <Country></Country>
    </Buyer>
    <Seller>
      <ILN></ILN>
      <PartyName></PartyName>
      <StreetAndNumber></StreetAndNumber>
      <CityName></CityName>
      <PostCode></PostCode>
      <Country></Country>
    </Seller>
    <DeliveryPoint>
      <ILN></ILN>
    </DeliveryPoint>
    <ShipFrom>
      <ILN></ILN>
    </ShipFrom>
  </OrderResponse-Parties>
  <OrderResponse-Lines>
    <Line>
      <Line-Item>
        <LineNumber>3</LineNumber>
        <BuyerItemCode>gesgrere</BuyerItemCode>
        <ItemDescription></ItemDescription>
        <ItemStatus></ItemStatus>
        <ItemType></ItemType>
        <OrderedQuantity>0</OrderedQuantity>
        <QuantityToBeDelivered>55</QuantityToBeDelivered>
        <QuantityDifference>0</QuantityDifference>
        <UnitOfMeasure></UnitOfMeasure>
        <OrderedUnitNetPrice>0</OrderedUnitNetPrice>
        <Discount>0</Discount>
        <ExpectedDeliveryDate>2014-02-12</ExpectedDeliveryDate>
      </Line-Item>
    </Line>
  </OrderResponse-Lines>
  <OrderResponse-Summary>
    <TotalLines>0</TotalLines>
  </OrderResponse-Summary>
</Document-OrderResponse>

2 个答案:

答案 0 :(得分:1)

SELECT
   '231' AS "OrderResponse-Header/OrderResponseType",
   'Const' AS "OrderResponse-Header/OrderResponseNumber", 
   COALESCE(CONVERT(VARCHAR(10), GETDATE(), 20), '') AS "OrderResponse-Header/OrderResponseDate",
   COALESCE(T.OrderNumber, '') AS "OrderResponse-Header/OrderNumber",
   COALESCE(CONVERT(VARCHAR(10), GETDATE(), 20), '') AS "OrderResponse-Header/OrderDate",
   '0' AS "OrderResponse-Header/DocumentFunctionCode",

   /* ... */

   (
      SELECT
         COALESCE(T1.LineNumber, '') AS "LineNumber",
         COALESCE(T1.BuyerItemCode, '') AS "BuyerItemCode",
         '' AS "ItemDescription",
         '' AS "ItemStatus",
         '' AS "ItemType",
         '0' AS "OrderedQuantity",
         COALESCE(T1.QuantityToBeDelivered, '') AS "QuantityToBeDelivered",
         '0' AS "QuantityDifference",
         '' AS "UnitOfMeasure",
         '0' AS "OrderedUnitNetPrice",
         '0' AS "Discount",
         COALESCE(CONVERT(VARCHAR(10), T1.ExpectedDeliveryDate, 20), '') AS "ExpectedDeliveryDate"
      FROM Import.OrderResponses AS T1
      WHERE T1.OrderNumber = T.OrderNumber
      FOR XML PATH('Line-Item'), ROOT('Line'), TYPE
   ) AS "OrderResponse-Lines",
   '0' AS "OrderResponse-Summary/TotalLines"
FROM Import.OrderResponses AS T
WHERE T.OrderNumber = 'Gr342'
GROUP BY T.OrderNumber
FOR XML PATH(''), ROOT('Document-OrderResponse'), TYPE

我不确定您是否要重复<Line>元素或<Line-Item>元素。我选择了<Line-Item>元素,但通过操纵FOR XML参数可以很容易地进行更改。

答案 1 :(得分:0)

代码:

select '' as "OrderResponse-Lines/Line/Line-Item/ItemDescription",
    '' as "OrderResponse-Lines/Line/Line-Item/ItemStatus",
    '' as "OrderResponse-Lines/Line/Line-Item/ItemType"
for xml path(''), root('Document-OrderResponse'), type;

结果:

<Document-OrderResponse>
  <OrderResponse-Lines>
    <Line>
      <Line-Item>
        <ItemDescription></ItemDescription>
        <ItemStatus></ItemStatus>
        <ItemType></ItemType>
      </Line-Item>
    </Line>
  </OrderResponse-Lines>
</Document-OrderResponse>

如您所见,创建了一个/Line/Line-Item个节点。我的@@version

  

Microsoft SQL Server 2012 - 11.0.5058.0(X64)2014年5月14日18:34:29     版权所有(c)Microsoft Corporation Developer Edition(64位)on   Windows NT 6.1(Build 7601:Service Pack 1)

编辑:啊,我明白了。看起来您的表中有重复的行。结果是什么:

select count(*) from Import.OrderResponses where T.OrderNumber = 'Gr342';