Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 24th, 2006, 02:55 PM
Suma
Guest
 
Posts: n/a
Default Simple aggregation using xquery

I dont know xquery , so i was thinking someone in the forum will help
me out with this easy question

Suppose I have an instance document given below
<Root>
<Author Name='Alice' NumberOfBooks = '1'>
<Author Name='Bob' NumberOfBooks = '4'>
<Author Name='Collin' NumberOfBooks = '3'>
<Author Name='David' NumberOfBooks = '2'>
</Root>

Is there a way i can get the total number of books published by all
authors using a single xquery statement. (similiar to say SQL - select
sum(col) semantics)

What i DO NOT WANT to do is gettting all the nodes and doing the sum
at the client/my application. I would rather have the XML engine handle
the aggregation


Suma

  #2  
Old July 24th, 2006, 02:55 PM
SL
Guest
 
Posts: n/a
Default Re: Simple aggregation using xquery

"Suma" a écrit :
Quote:
I dont know xquery , so i was thinking someone in the forum will help
me out with this easy question
>
Suppose I have an instance document given below
<Root>
<Author Name='Alice' NumberOfBooks = '1'>
<Author Name='Bob' NumberOfBooks = '4'>
<Author Name='Collin' NumberOfBooks = '3'>
<Author Name='David' NumberOfBooks = '2'>
</Root>
>
Is there a way i can get the total number of books published by all
authors using a single xquery statement. (similiar to say SQL - select
sum(col) semantics)
sum(/Root/Author/@NumberOfBookds)
  #3  
Old July 24th, 2006, 03:55 PM
Suma
Guest
 
Posts: n/a
Default Re: Simple aggregation using xquery

Great thanks
Now what if i need to do a aggregate over groups?
My repro was simpler than what i intend to do
<Root>
<Author ="Alice" Attrib="1">
<Author ="Alice" Attrib="2">
<Author ="Bob" Attrib="3">
<Author ="Bob" Attrib="4">
</Root>

I need to return
Alice 3
Bob 7
and
so on
Suma


SL wrote:
Quote:
"Suma" a écrit :
Quote:
I dont know xquery , so i was thinking someone in the forum will help
me out with this easy question

Suppose I have an instance document given below
<Root>
<Author Name='Alice' NumberOfBooks = '1'>
<Author Name='Bob' NumberOfBooks = '4'>
<Author Name='Collin' NumberOfBooks = '3'>
<Author Name='David' NumberOfBooks = '2'>
</Root>

Is there a way i can get the total number of books published by all
authors using a single xquery statement. (similiar to say SQL - select
sum(col) semantics)
sum(/Root/Author/@NumberOfBookds)
  #4  
Old July 24th, 2006, 05:15 PM
SL
Guest
 
Posts: n/a
Default Re: Simple aggregation using xquery

"Suma" a écrit :
Quote:
Great thanks
Now what if i need to do a aggregate over groups?
My repro was simpler than what i intend to do
<Root>
<Author ="Alice" Attrib="1">
<Author ="Alice" Attrib="2">
<Author ="Bob" Attrib="3">
<Author ="Bob" Attrib="4">
^^^
This is not well-formed XML. I assume you have:
<Author name="Bob" Attrib="4">
^^^^
instead
Quote:
</Root>
Quote:
I need to return
Alice 3
Bob 7
and
so on
Suma
It is an XPath issue :

sum(/Root/Author[@name='Alice']/@Attrib)

If you want to loop over all possible names, XQuery provide many
functionnalities, such as the so-called "Flower expression":

for $author in Book/Author/@name
return
<nbrOfBooks>{sum(/Root/Author[@name=$author]/@Attrib)}</nbrOfBooks>

(I havn't practiced XQuery for some time, and this is not tested!)
  #5  
Old July 24th, 2006, 05:15 PM
SL
Guest
 
Posts: n/a
Default Re: Simple aggregation using xquery


Sorry:

- for $author in Book/Author/@name
+ for $author in /Book/Author/@name
return
- <nbrOfBooks>{sum(/Root/Author[@name=$author]/@Attrib)}</nbrOfBooks>
+ <nbrOfBooks>{sum(/Book/Author[@name=$author]/@Attrib)}</nbrOfBooks>
 

Bookmarks

« modulo | DTD Parser »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles