[英]T-SQL combine based on values with for XML path
我陷入了無法解決的問題。 我有一些模擬數據來模擬:
DECLARE @tbl TABLE (orderReference VARCHAR(50), shipmentReference VARCHAR(50), additionalComments VARCHAR(200))
DECLARE @finalXML XML
INSERT INTO @tbl
VALUES
('1234567890', 'shipmentRef1', 'Some additional text'),
('1234567890', 'shipmentRef2', 'Some additional text for shipment ref 2'),
('0987654321', 'shipmentRef999', 'Some additional text for shipment ref 999')
1 個包含 3 列的表格(orderReference、shipmentReference 和 additionalComments):
訂單參考 | 出貨參考 | 補充評論 |
---|---|---|
1234567890 | 裝運編號 1 | 一些額外的文字 |
1234567890 | 裝運編號 2 | 一些額外的文本參考 2 |
0987654321 | 裝運編號999 | 裝運編號 999 的一些附加文本 |
使用此表,我正在創建一個新的 XML 變量來構建我將要使用的消息:
SET @finalXML = (
SELECT
"description" = 'This is on top level',
"transportbookings" = (
SELECT
"Order_reference" = i.orderReference,
"shipments" = (
SELECT
"reference" = i.shipmentReference
FOR XML PATH('shipment'), TYPE
)
FROM @tbl i
FOR XML PATH('transportbooking'), TYPE
)
FOR XML PATH ('import'), TYPE
)
此時它正在創建一個 XML,如下所示:
<import>
<description>This is on top level</description>
<transportbookings>
<transportbooking>
<Order_reference>0987654321</Order_reference>
<shipments>
<shipment>
<reference>shipmentRef999</reference>
</shipment>
</shipments>
</transportbooking>
<transportbooking>
<Order_reference>1234567890</Order_reference>
<shipments>
<shipment>
<reference>shipmentRef1</reference>
</shipment>
</shipments>
</transportbooking>
<transportbooking>
<Order_reference>1234567890</Order_reference>
<shipments>
<shipment>
<reference>shipmentRef2</reference>
</shipment>
</shipments>
</transportbooking>
</transportbookings>
</import>
我試圖實現有 2 個基於“orderReference”值的“transportbooking”元素。 因此它需要為“1234567890”創建 1 個“transportbooking”元素,其中包含 2 個“shipments”元素。
所以預期的 output 應該是:
<import>
<description>This is on top level</description>
<transportbookings>
<transportbooking>
<Order_reference>0987654321</Order_reference>
<shipments>
<shipment>
<reference>shipmentRef999</reference>
</shipment>
</shipments>
</transportbooking>
<transportbooking>
<Order_reference>1234567890</Order_reference>
<shipments>
<shipment>
<reference>shipmentRef1</reference>
</shipment>
<shipment>
<reference>shipmentRef2</reference>
</shipment>
</shipments>
</transportbooking>
</transportbookings>
</import>
我嘗試使用外部應用,將“FROM @tbl i”移動到另一個元素,嘗試“GROUP BY”orderReference 列。
我手忙腳亂,有沒有人為我開眼界?
請嘗試以下解決方案。
SQL
DECLARE @tbl TABLE (orderReference VARCHAR(50), shipmentReference VARCHAR(50), additionalComments VARCHAR(200));
INSERT INTO @tbl VALUES
('1234567890', 'shipmentRef1', 'Some additional text'),
('1234567890', 'shipmentRef2', 'Some additional text for shipment ref 2'),
('0987654321', 'shipmentRef999', 'Some additional text for shipment ref 999');
DECLARE @finalXML XML =
(SELECT 'This is on top level' AS [description]
, (
SELECT p.orderReference AS [Order_reference]
, (SELECT c.shipmentReference AS [reference]
FROM @tbl AS c
WHERE p.orderReference = c.orderReference
FOR XML PATH('shipment'), TYPE, ROOT('shipments'))
FROM @tbl AS p
GROUP BY orderReference
FOR XML PATH('transportbooking'), TYPE, ROOT('transportbookings'))
FOR XML PATH(''), TYPE, ROOT('import'));
-- test
SELECT @finalXML;
Output XML
<import>
<description>This is on top level</description>
<transportbookings>
<transportbooking>
<Order_reference>0987654321</Order_reference>
<shipments>
<shipment>
<reference>shipmentRef999</reference>
</shipment>
</shipments>
</transportbooking>
<transportbooking>
<Order_reference>1234567890</Order_reference>
<shipments>
<shipment>
<reference>shipmentRef1</reference>
</shipment>
<shipment>
<reference>shipmentRef2</reference>
</shipment>
</shipments>
</transportbooking>
</transportbookings>
</import>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.