[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

Getting a count of values that show up at least once

Andrew Serff lists at serff.net
Tue May 18 13:21:51 PDT 2010


  Getting a count of values that show up at least once
Hello,

I have some example metrics that I'm using to model real metrics we will be
generating from our application and I'm trying to build some xqueries to
help extract the data we want from the metrics, but I'm having problems
wrapping my head around this one.  Here is some of the sample metrics:

<metrics>
    <metric>
        <hostname><![CDATA[myhost]]></hostname>
        <category><![CDATA[FIGHT MOVE]]></category>
        <name><![CDATA[Martial Art Action 0]]></name>
        <description><![CDATA[chucknorris just landed a slap on
chucknorris]]></description>
        <time_elapsed>36800</time_elapsed>
        <application><![CDATA[UFC]]></application>
        <action><![CDATA[SLAP]]></action>
        <timestamp>2010-05-14T09:02:33</timestamp>
        <user><![CDATA[chucknorris]]></user>
    </metric>
    <metric>
        <hostname><![CDATA[myhost]]></hostname>
        <category><![CDATA[FIGHT MOVE]]></category>
        <name><![CDATA[Martial Art Action 1]]></name>
        <description><![CDATA[jetlee just landed a roundhouse on
chucknorris]]></description>
        <time_elapsed>35245</time_elapsed>
        <application><![CDATA[UFC]]></application>
        <action><![CDATA[ROUNDHOUSE]]></action>
        <timestamp>2010-05-14T09:02:33</timestamp>
        <user><![CDATA[jetlee]]></user>
    </metric>
    <metric>
        <hostname><![CDATA[myhost]]></hostname>
        <category><![CDATA[FIGHT MOVE]]></category>
        <name><![CDATA[Martial Art Action 2]]></name>
        <description><![CDATA[chucknorris just landed a kick on
jetlee]]></description>
        <time_elapsed>43474</time_elapsed>
        <application><![CDATA[UFC]]></application>
        <action><![CDATA[KICK]]></action>
        <timestamp>2010-05-14T09:02:33</timestamp>
        <user><![CDATA[chucknorris]]></user>
    </metric>
</metrics>

What I would like to pull out of this is how many unique users there were
per action (i.e. How many people Kicked someone today).  So I've stared of
by getting a list of all the unique Actions:
for $value in
(distinct-values(collection('metricsCollection.xml')//metrics/metric/action))
and a list of all the unique users for that action:
for $user in
(distinct-values(collection('metricsCollection.xml')//metrics/metric[action
= $value]/user))

That is where I get stuck.  I just want to count which users show up at
least once and then output something like:
<mertics>
  <metric>
    <action>KICK</action>
    <num_users>5</num_users>
  </metric>
  <metric>
    <action>SLAP</action>
    <num_users>2</num_users>
  </metric>
</metrics>

if I do:
count(collection('metricsCollection.xml')//metrics/metric[action = $value
and user = $user])
I just get a count of all the nodes that have that value and user.  I've
been trying to figure out how to just add 1 to some counter like you would
in java for example, but it seems you can't increment values in xquery
because once you define a variable it never changes.

Does anyone have any hints/advice for me?

Thanks!
Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://x-query.com/pipermail/talk/attachments/20100518/3fc85f3e/attachment.htm


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.