简体   繁体   English

SQL Server:按元素名称计数的XML元素

[英]SQL Server : Count of XML Elements By Element Name

I have successfully obtained a count of sub elements using this query. 我已经使用此查询成功获得了一些子元素。

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

However, I want to group this count by the actual name of the sub element. 但是,我想将此计数按子元素的实际名称分组。

My XML looks something like this: 我的XML看起来像这样:

Record 1: 记录1:

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

Record 2: 记录2:

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

Record 3: 记录3:

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

Not only do I need to pull the count of each bureau, but I need to get the name of the bureau (BureauRed, BureauGreen, BureauBlue, etc.) When I try to group by count I get the error: 我不仅需要提取每个局的计数,还需要获得局的名称(BureauRed,BureauGreen,BureauBlue等)。当我尝试按计数分组时,我得到了错误:

XML methods are not allowed in a GROUP BY clause. GROUP BY子句中不允许使用XML方法。

Using this query: 使用此查询:

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

How do I get the count and the name of the bureau? 我如何获得局的数目和名称?

I'm looking for this result: 我正在寻找这个结果:

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

You need to extract the elements that are children of Root/ThirdParty first and use that derived table to do the counting. 您需要首先提取作为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;

You can group after recieving data via XQuery: 您可以在通过XQuery接收数据后进行分组:

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;

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM