To make the awareness of the longtime addition of the Group by Clause supplied by Boris Verhaegen already in November 2006 to eXist-db bigger, I give you Boris's short but clean example of its use. This posting is the first step in order to close bug #3165906 about documenting this feature.
let $g-b-data := <items>
<item>
<key1>1</key1>
<key2>a</key2>
</item>
<item>
<key1>1</key1>
<key2>b</key2>
</item>
<item>
<key1>0</key1>
<key2>c</key2>
</item>
<item>
<key1>0</key1>
<key2>d</key2>
</item>
</items>
(: grouping query :)
return
for $item in $g-b-data//item
group $item as $partition by $item/key1 as $key1
return
<group>
{$key1,$partition}
</group>
Which gives the following result:
<group>
<key1>1</key1>
<item>
<key1>1</key1>
<key2>a</key2>
</item>
<item>
<key1>1</key1>
<key2>b</key2>
</item>
</group>
<group>
<key1>0</key1>
<item>
<key1>0</key1>
<key2>c</key2>
</item>
<item>
<key1>0</key1>
<key2>d</key2>
</item>
</group>