Skip to content

Instantly share code, notes, and snippets.

@mblakele
Created November 6, 2011 01:02
Show Gist options
  • Save mblakele/1342291 to your computer and use it in GitHub Desktop.
Save mblakele/1342291 to your computer and use it in GitHub Desktop.
W3C XQuery 3.0 - Group By Use Cases Q2, using map:map
<result>{
let $m := map:map()
let $build :=
for $sales in doc("sales-records.xml")/*/record
let $state := doc("stores.xml")/*/store[
store-number = $sales/store-number]/state
let $category := doc("products.xml")/*/product[
name = $sales/product-name]/category
let $key := concat($state, '|', $category)
return map:put(
$m, $key, sum((
map:get($m, $key),
$sales/qty)))
for $key in map:keys($m)
let $state := element state {
substring-before($key, '|') }
let $category := element category {
substring-after($key, '|') }
order by $state, $category
return
<group>
{$state, $category}
<total-qty>{map:get($m, $key)}</total-qty>
</group>
}</result>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment