using xquery to count employees in department (same lvl tag)

92 Views Asked by At

I have a xml file like this:

<employees>
    <emp_num>
        <department>1</department>
        <salary>1000</salary>
    </emp_mum>
    <emp_num>
        <department>1</department>
        <salary>2000</salary>
    </emp_mum>
</employees>

I want to know the number of employees per department ordered by the average salary of the department.

My main problem to do this is that "department" and "salary" are at the same level and I don't know how to use FLWOR in this situation.

1

There are 1 best solutions below

2
On BEST ANSWER

Try

for $emp in //emp_num
group by $d := $emp/department
order by avg($emp/salary)
return count($emp)