2017-09-06 97 views
0

我已成功獲取使用此查詢的子元素的計數。SQL Server:按元素名稱計數XML元素

SELECT 
    raa.ApplicationXML.value('count(Root/ThirdParty/*)', 'Int') as [Bureau Count] 
FROM 
    Table1 raa 

但是,我想通過子元素的實際名稱對這個計數進行分組。

我的XML看起來是這樣的:

記錄1:

<Root> 
    <ThirdParty> 
     <BureauRed /> 
     <BureauGreen /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root> 

記錄2:

<Root> 
    <ThirdParty> 
     <BureauRed /> 
     <BureauPurple /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root> 

記錄3:

<Root> 
    <ThirdParty> 
     <BureauGreen /> 
     <BureauRed /> 
     <BureauPurple /> 
     <BureauOrange /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root> 

我不僅需要拉各自的數量局,但我需要得到局(BureauRed,BureauGreen,BureauBlue等)的名稱,當我嘗試按計數我得到的錯誤:

XML methods are not allowed in a GROUP BY clause.

使用此查詢:

SELECT 
    raa.ApplicationXML.value('count(Root/ThirdParty/*)', 'Int') as [Bureau Count] 
FROM 
    Table1 raa 
GROUP BY 
    raa.ApplicationXML.value('count(Root/ThirdParty/*)', 'Int') 

我如何得到主席團的人數和名稱?

我在尋找這樣的結果:

Bureau   Count 
============ ===== 
BureauRed  3 
BureauGreen 2 
BureauBlue  3 
BureauPurple 2 
BureauOrange 1 
+0

對於那些三個記錄 –

+0

這是更好地給我們的樣本數據作爲填充臨時表提供預期的結果 –

+0

我更新了故事;對不起,這些號碼沒有排隊。 – user3179585

回答

0

您可以將通過XQuery的recieving數據後:

DECLARE @xml1 xml = 
'<Root> 
    <ThirdParty> 
     <BureauRed /> 
     <BureauGreen /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root>'; 
DECLARE @xml2 xml = 
'<Root> 
    <ThirdParty> 
     <BureauRed /> 
     <BureauPurple /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root>'; 
DECLARE @xml3 xml = 
'<Root> 
    <ThirdParty> 
     <BureauGreen /> 
     <BureauRed /> 
     <BureauPurple /> 
     <BureauOrange /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root>'; 

DECLARE @temp TABLE (XmlData xml); 

INSERT @temp VALUES (@xml1), (@xml2), (@xml3); 

WITH cte AS 
(
    SELECT n.value('local-name(.)', 'varchar(50)') nodeName 
     FROM @temp t 
     CROSS APPLY t.XmlData.nodes('/Root/ThirdParty/*') x(n) 
) 
SELECT nodeName, count(*) cnt 
    FROM cte 
    GROUP BY nodeName; 

輸出:

nodeName           cnt 
-------------------------------------------------- ----------- 
BureauBlue           3 
BureauGreen          2 
BureauOrange          1 
BureauPurple          2 
BureauRed           3 
0

你需要提取是派生表做計數Root/ThirdParty第一和利用兒童的元素。

DECLARE @tv TABLE(x XML); 

INSERT INTO @tv(x)VALUES(
N'<Root> 
    <ThirdParty> 
     <BureauRed /> 
     <BureauGreen /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root>'),(
N'<Root> 
    <ThirdParty> 
     <BureauRed /> 
     <BureauPurple /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root>'),(
N'<Root> 
    <ThirdParty> 
     <BureauGreen /> 
     <BureauRed /> 
     <BureauPurple /> 
     <BureauOrange /> 
     <BureauBlue /> 
    </ThirdParty> 
</Root>'); 

WITH bureaus AS (
    SELECT 
     bureau=n.v.value('local-name(.)','NVARCHAR(256)') 
    FROM 
     @tv 
     CROSS APPLY x.nodes('/Root/ThirdParty/*') AS n(v) 
) 
SELECT 
    bureau, 
    [count]=COUNT(*) 
FROM 
    bureaus 
GROUP BY 
    bureau 
ORDER BY 
    bureau;