File: Data.xml
File: Query.xquery
let $allemployees := doc("Data.xml")//employee
for $d in distinct-values($allemployees/@salary)
for $n in distinct-values($allemployees[@salary = $d]/@yearServed)
let $employees := $allemployees[@salary = $d and @yearServed = $n]
order by $d, $n
return yearServed="{$n}"
numemployees="{count($employees)}"
total="{sum($employees/@yearServed)}"/>
Output: