2017-06-22 65 views
1

暈所有,使用UNION刪除重複的日期,但保留相同的數據

我希望你能幫助我。我正在使用SQL Server。 我有這樣的XML數據:

<Root> 
    <Header> 
    <refNo>S350282535-100060</refNo> 
    <doNumber>0</doNumber> 
    </Header> 
    <Header> 
    <refNo>S350282535-100061</refNo> 
    <doNumber>1</doNumber> 
    </Header> 
    <Detail> 
    <item>114495</item> 
    <qty>2</qty> 
    <uom>PC</uom> 
    <refNo>S350282535-100060</refNo> 
    <remarks>-</remarks> 
    <reason>-</reason> 
</Detail> 
<Detail> 
    <item>114496</item> 
    <qty>2</qty> 
    <uom>PC</uom> 
    <refNo>S350282535-100061</refNo> 
    <remarks>-</remarks> 
    <reason>-</reason> 
</Detail> 
<Detail> 
    <item>114497</item> 
    <qty>2</qty> 
    <uom>PC</uom> 
    <refNo>S350282535-100061</refNo> 
    <remarks>-</remarks> 
    <reason>-</reason> 
</Detail> 
</Root> 

我怎麼能得到這樣的數據:

refNo     doNumber Item  qty uom 
-------------------------------------------------------------------- 
S350282535-100060  0   114495  2  PC 
S350282535-100061  1   114496  2  PC 
S350282535-100061  1   114497  2  PC 

這裏是我的疑問,我已經嘗試過:

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX), @flagParameter AS 
INT 

SELECT @XML = XMLData FROM XMLwithOpenXML 

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML 
SET @flagParameter = 2 

SELECT item, qty,uom, refNo, remarks, reason 
FROM OPENXML(@hDoc, 'Root/Detail',@flagParameter) 
WITH 
(
    item [int], 
    qty [int], 
    uom [nvarchar] (6), 
    refNo [nvarchar] (50), 
    remarks [nvarchar] (100), 
    reason [nvarchar] (100) 
) 

SELECT refNo, doNumber 
FROM OPENXML(@hDoc, 'Root/Header',@flagParameter) 
WITH 
(
refNo [nvarchar](50), 
doNumber [int] 
) 

SELECT refNo , doNumber, item--, qty,uom, remarks, reason 
FROM OPENXML(@hDoc, 'Root/Detail',@flagParameter) 
WITH 
(
refNo [nvarchar] (50), 
doNumber [int], 
item [int], 
qty [int], 
uom [nvarchar] (6), 
remarks [nvarchar] (100), 
reason [nvarchar] (100) 
) 

UNION 

SELECT refNo, doNumber, One as Item--, two as two, three as three, four as 
four 
FROM OPENXML(@hDoc, 'Root/Header',@flagParameter) 
WITH 
(
refNo [nvarchar](50), 
doNumber [int], 
one [int], 
two [int], 
three [int], 
four [int] 
) EXEC sp_xml_removedocument @hDoc 

我使用聯盟,但結果並不像我預期的那樣,有沒有人可以幫助我?我會感謝你的幫助。

+1

請您分享您嘗試的UNION查詢嗎? –

+0

是的,當然@keyur: – AgusmanPriantoro

+0

我在我的帖子中添加了我的查詢。 @keyur – AgusmanPriantoro

回答

0

假設XML數據存儲在一個名爲@xml XML變量:

DECLARE @xml XML = '<Root> 
    <Header> 
    <refNo>S350282535-100060</refNo> 
    <doNumber>0</doNumber> 
    </Header> 
    <Header> 
    <refNo>S350282535-100061</refNo> 
    <doNumber>1</doNumber> 
    </Header> 
    <Detail> 
    <item>114495</item> 
    <qty>2</qty> 
    <uom>PC</uom> 
    <refNo>S350282535-100060</refNo> 
    <remarks>-</remarks> 
    <reason>-</reason> 
</Detail> 
<Detail> 
    <item>114496</item> 
    <qty>2</qty> 
    <uom>PC</uom> 
    <refNo>S350282535-100061</refNo> 
    <remarks>-</remarks> 
    <reason>-</reason> 
</Detail> 
<Detail> 
    <item>114497</item> 
    <qty>2</qty> 
    <uom>PC</uom> 
    <refNo>S350282535-100061</refNo> 
    <remarks>-</remarks> 
    <reason>-</reason> 
</Detail> 
</Root>' 

下面是一個可能的方式使用XML methods來獲得期望的結果和XPath/XQuery的,而不是舊的OPENXML:

;WITH Header(refNo, doNumber) 
AS 
(
    SELECT 
     T.C.value('refNo[1]', 'varchar(100)') 
     ,T.C.value('doNumber[1]', 'int') 
    FROM @xml.nodes('/Root/Header') AS T(C) 
) 

SELECT 
    Header.* 
    ,T.C.value('item[1]', 'varchar(100)') 'item' 
    ,T.C.value('qty[1]', 'int') 'qty' 
    ,T.C.value('uom[1]', 'varchar(100)') 'uom' 
FROM @xml.nodes('/Root/Detail') AS T(C) 
INNER JOIN Header on T.C.value('refNo[1]', 'varchar(100)') = refNo 

demo

+0

非常感謝你,它的功能就像一個魔法。 – AgusmanPriantoro

+0

@AgusmanPriantoro歡迎您。由於你迄今從未做過,所以我會提醒你考慮[接受答案](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)如果它解決了有問題的問題。 – har07

相關問題