gpt4 book ai didi

sql - 使用 SQL 查询包含重复数据的 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:21:58 29 4
gpt4 key购买 nike

我需要帮助使用 T-SQL (2008/2012) 查询包含重复数据的 XML 文件。

XML 代码如下。它包含一些摘要级别节点(VENDOR_NAME、VENDOR_NUMBER、SUBTOTAL),然后是详细级别的行(在 NONPO 容器节点中),然后是一组进一步的详细信息行(在 NONPO2 容器节点中)。

<Document ID="11">
<Version>4.7</Version>
<LastModifiedInVersion>4.7</LastModifiedInVersion>
<Controls>
<Control ID="VENDOR_NAME">
<Value>CVS</Value>
</Control>
<Control ID="VENDOR_NUMBER">
<Value>1101</Value>
</Control>
</Controls>
<Container ID="NONPO">
<Header />
<Row>
<Control ID="INVOICE_NUMBER">
<Value>23126</Value>
</Control>
<Control ID="INVOICE_DATE">
<Value>05/13/2014</Value>
</Control>
<Container ID="NONPO2">
<Header />
<Row>
<Control ID="DESCRIPTION">
<Value>Pens</Value>
</Control>
<Control ID="AMOUNT">
<Value>50.32</Value>
</Control>
</Row>
<Row>
<Control ID="DESCRIPTION">
<Value>Tape</Value>
</Control>
<Control ID="AMOUNT">
<Value>60.00</Value>
</Control>
</Row>
<Footer>
<Control ID="INV_SUBTOTAL">
<Value>110.32</Value>
</Control>
</Footer>
</Container>
</Row>
<Row>
<Control ID="INVOICE_NUMBER">
<Value>61626</Value>
</Control>
<Control ID="INVOICE_DATE">
<Value>06/01/2014</Value>
</Control>
<Container ID="NONPO2">
<Header />
<Row>
<Control ID="DESCRIPTION">
<Value>Microsoft Office</Value>
</Control>
<Control ID="AMOUNT">
<Value>1600.00</Value>
</Control>
</Row>
<Footer>
<Control ID="INV_SUBTOTAL">
<Value>1600.00</Value>
</Control>
</Footer>
</Container>
</Row>
<Footer>
<Control ID="SUBTOTAL">
<Value>1710.32</Value>
</Control>
</Footer>
</Container>
<AutoKeys />
</Document>

我希望得到类似于以下输出的结果:

VENDOR_NAME     VENDOR_NUMBER   INVOICE_NUMBER  INVOICE_DATE    DESCRIPTION         AMOUNT  INV_SUBTOTAL    SUBTOTAL
CVS 1101 23126 5/13/2014 Pens 50.32 110.32 1710.32
CVS 1101 23126 5/13/2014 Tape 60 110.32 1710.32
CVS 1101 61626 6/1/2014 Microsoft Office 1600 1600 1710.32

最佳答案

select @XML.value('(/Document/Controls/Control[@ID = "VENDOR_NAME"]/Value/text())[1]', 'nvarchar(100)') as VENDOR_NAME,
@XML.value('(/Document/Controls/Control[@ID = "VENDOR_NUMBER"]/Value/text())[1]', 'int') as VENDOR_NUMBER,
R1.X.value('(Control[@ID = "INVOICE_NUMBER"]/Value/text())[1]', 'int') as INVOICE_NUMBER,
R2.X.value('(Control[@ID = "DESCRIPTION"]/Value/text())[1]', 'nvarchar(max)') as DESCRIPTION,
R2.X.value('(Control[@ID = "AMOUNT"]/Value/text())[1]', 'nvarchar(max)') as AMOUNT,
C2.X.value('(Footer/Control[@ID = "INV_SUBTOTAL"]/Value/text())[1]', 'nvarchar(max)') as INV_SUBTOTAL,
C1.X.value('(Footer/Control[@ID = "SUBTOTAL"]/Value/text())[1]', 'nvarchar(max)') as SUBTOTAL
from @XML.nodes('/Document/Container') as C1(X)
cross apply C1.X.nodes('Row') as R1(X)
cross apply R1.X.nodes('Container') as C2(X)
cross apply C2.X.nodes('Row') as R2(X)

SQL Fiddle

关于sql - 使用 SQL 查询包含重复数据的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23544008/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com