在 XML 中查找重复项并将其与相关信息一起返回
Finding duplicates and returning them with related information in XML
以下查询returns具有给定订单号的所有订单。我如何重写它以便返回指定的重复订单 ID 及其各自的 custid and/or 公司名称?
DECLARE @x AS XML;
SET @x = N'
<CustomersOrders>
<Customer custid="1">
<companyname>Customer NABCD</companyname>
<Order orderid="10692">
<orderdate>2013-11-03</orderdate>
</Order>
<Order orderid="10702">
<orderdate>2007-10-13</orderdate>
</Order>
<Order orderid="10693">
<orderdate>2008-03-16</orderdate>
</Order>
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
<Order orderid="10308">
<orderdate>2006-09-18</orderdate>
</Order>
<Order orderid="10693">
<orderdate>2008-03-04</orderdate>
</Order>
</Customer>
</CustomersOrders>';
SELECT @x.query('//Order[@orderid=10693]')
编辑:
输出格式可以是...
<CustomersOrders>
<Customer custid="1">
<companyname>Customer NABCD</companyname>
<Order orderid="10693"/>
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
<Order orderid="10693"/>
</Customer>
</CustomersOrders>
或(更好)
<CustomersOrders>
<Order orderid="10693">
<Customer custid="1">
<companyname>Customer NABCD</companyname>
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
</Customer>
</Order>
</CustomersOrders>'
这是一种可能的方式:
SELECT @x.query('
<CustomersOrders>
{
for $order in //Order[@orderid=10693]
let $customer := $order/..
return
<Customer custid="{$customer/@custid}">
{$customer/companyname}
<Order orderid="{$order/@orderid}"/>
</Customer>
}
</CustomersOrders>
')
输出:
<CustomersOrders>
<Customer custid="1">
<companyname>Customer NABCD</companyname>
<Order orderid="10693" />
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
<Order orderid="10693" />
</Customer>
</CustomersOrders>
以下查询returns具有给定订单号的所有订单。我如何重写它以便返回指定的重复订单 ID 及其各自的 custid and/or 公司名称?
DECLARE @x AS XML;
SET @x = N'
<CustomersOrders>
<Customer custid="1">
<companyname>Customer NABCD</companyname>
<Order orderid="10692">
<orderdate>2013-11-03</orderdate>
</Order>
<Order orderid="10702">
<orderdate>2007-10-13</orderdate>
</Order>
<Order orderid="10693">
<orderdate>2008-03-16</orderdate>
</Order>
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
<Order orderid="10308">
<orderdate>2006-09-18</orderdate>
</Order>
<Order orderid="10693">
<orderdate>2008-03-04</orderdate>
</Order>
</Customer>
</CustomersOrders>';
SELECT @x.query('//Order[@orderid=10693]')
编辑:
输出格式可以是...
<CustomersOrders>
<Customer custid="1">
<companyname>Customer NABCD</companyname>
<Order orderid="10693"/>
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
<Order orderid="10693"/>
</Customer>
</CustomersOrders>
或(更好)
<CustomersOrders>
<Order orderid="10693">
<Customer custid="1">
<companyname>Customer NABCD</companyname>
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
</Customer>
</Order>
</CustomersOrders>'
这是一种可能的方式:
SELECT @x.query('
<CustomersOrders>
{
for $order in //Order[@orderid=10693]
let $customer := $order/..
return
<Customer custid="{$customer/@custid}">
{$customer/companyname}
<Order orderid="{$order/@orderid}"/>
</Customer>
}
</CustomersOrders>
')
输出:
<CustomersOrders>
<Customer custid="1">
<companyname>Customer NABCD</companyname>
<Order orderid="10693" />
</Customer>
<Customer custid="2">
<companyname>Customer HMKLI</companyname>
<Order orderid="10693" />
</Customer>
</CustomersOrders>