If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Monday, March 18, 2013

XQuery3.0 Group by example

return
<groups>
{
for $person in $persons/person
group by
$country := $person/country,
$gender := $person/@gender
return
<group country="{$country}" gender="{$gender}">
<averageAge>{avg($person/age)}</averageAge>
</group>
}
</groups>
(:
**********************************************
Logical result
**********************************************
Belgium Netherlands
Male Female Male Female
---------------------------------------------
A 10 C 80 B 20 E 30
D 40 F 36
---- ---- ---- -----
25 80 28 30
**********************************************
XQuery output:
**********************************************
<groups>
<group country="Belgium" gender="male">
<averageAge>25</averageAge>
</group>
<group country="Belgium" gender="female">
<averageAge>80</averageAge>
</group>
<group country="Netherlands" gender="male">
<averageAge>28</averageAge>
</group>
<group country="Netherlands" gender="female">
<averageAge>30</averageAge>
</group>
</groups>
:)

2 comments:

  1. You're still helping me even though you're not here anymore :-D

    ReplyDelete
    Replies
    1. Good to see.... Soon I will be posting more articles :) But this time probably more related to data science

      Delete