saxonica.com

Grouping

A subset of the grouping syntax from the draft XQuery 1.1 specification is implemented. The group by clause must be preceded in the FLWOR expression by (a) a single for clause, which selects the sequence to be grouped, and (b) a single let clause, which defines the grouping key; the "group by" clause must name the variable that is declared in the let clause. For example: for $x in //employee let $k := $x/department group by $k return .... Within the return clause, $x refers to the content of the current group, and $k to the current grouping key.

The clauses where and order by may optionally appear after the group by. The sequence of clauses must therefore be: FOR LET GROUP-BY WHERE? ORDER-BY? RETURN.

The following example groups employees by location, considering only those where the location is non-null:


for $e in //employee
let $loc := $e/location
group by $loc
where exists($oc)
order by count($e)
return <location name="{$loc}" count="{count($e)}">{$e}</location>

Next