? on Count specific value on XML data

  • Hi

    How do I find a count of a specific value in XML?

    data.value('count(/SampleXML/Colors/*)', 'int') finds all

    so say Black in [1] and Red in [2] and Black in [3]

    How do I get Black 2. Red 1

     

    Thanks

     

     

  • jbalbo wrote:

    Hi

    How do I find a count of a specific value in XML?

    data.value('count(/SampleXML/Colors/*)', 'int') finds all

    so say Black in [1] and Red in [2] and Black in [3]

    How do I get Black 2. Red 1

    Thanks

    If you want some code, please provide some sample XML to code against.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Thnaks and sorry for not adding this

    So in this example I would want Black 2 and red 1

    <Data>

    <Others>

    <members>

    <Name>adf</Name>

    <Color>red</Minor>

    </members>

    <members>

    <Name>adf</Name>

    <Color>black</Minor>

    </members>

    <members>

    <Name>adf</Name>

    <Color>black</Minor>

    </members>

    </Others>

    </Data>

  • jbalbo wrote:

    Thnaks and sorry for not adding this

    So in this example I would want Black 2 and red 1

    <Data> <Others> <members> <Name>adf</Name> <Color>red</Minor> </members> <members> <Name>adf</Name> <Color>black</Minor> </members> <members> <Name>adf</Name> <Color>black</Minor> </members> </Others> </Data>

    This is not valid XML. There is no opening Minor tag.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Sorry, I was trying not to show some data

    here is a samle I found and changed, so lets say in this example I want counts of name

    <Employees>

    <Person>

    <ID>1000</ID>

    <minor>yes</minor>

    </Person>

    <Person>

    <ID>1001</ID>

    <minor>yes</minor>

    </Person>

    <Person>

    <ID>1002</ID>

    <minor>yes</minor>

    </Person>

    <Person>

    <ID>1003</ID>

    <minor>no</minor>

    </Person>

    </Employees>

    So I would like the output to counts of minor = 'yes', disregarding ID

    my output would be Minor = 3

     

     

     

  • In case it helps others, here is a formatted version.

    <Employees>
    <Person>
    <ID>1000</ID>
    <minor>yes</minor>
    </Person>
    <Person>
    <ID>1001</ID>
    <minor>yes</minor>
    </Person>
    <Person>
    <ID>1002</ID>
    <minor>yes</minor>
    </Person>
    <Person>
    <ID>1003</ID>
    <minor>no</minor>
    </Person>
    </Employees>

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Thanks.... 🙂

  • Not totally clear what you want, but this gives you counts from your XML

    declare @x xml = '
    <Employees>
    <Person>
    <ID>1000</ID>
    <minor>yes</minor>
    </Person>
    <Person>
    <ID>1001</ID>
    <minor>yes</minor>
    </Person>
    <Person>
    <ID>1002</ID>
    <minor>yes</minor>
    </Person>
    <Person>
    <ID>1003</ID>
    <minor>no</minor>
    </Person>
    </Employees>
    ';

    select @x.query('
    for $value in distinct-values(/Employees/Person/minor)
    let $count := count(/Employees/Person/minor[. eq $value])
    return concat($value," ",xs:string($count))
    ');


    select @x.query('count(/Employees/Person/minor[. eq "yes"])');

    • This reply was modified 3 years, 9 months ago by  Mark Cowne. Reason: Typo

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply