У меня есть следующий запрос для получения различных функций для сотрудника: с
employeeScopeFunctions as (
select e.employeeId,
es.FunctionId,
ef.Label,
c.CompanyName,
realOrder = row_number() over(
partition by e.employeeId
order by isnull(es.sortOrder, 9999)
)
from employee e
LEFT JOIN employee_scope es on es.employeeId = e.employeeId
LEFT JOIN employee_function ef on es.FunctionId = ef.FunctionId
LEFT JOIN Company c ON es.CompanyId = c.ID
)
select *,
primacy = iif(realOrder = 1, 'main', 'secondary')
from employeeScopeFunctions
Для EmployeeId = 54 выдается следующее:
EmployeeId FunctionId Label CompanyName realOrder Primacy
54 273 Group Chief Executive Officer C1 1 primary
54 273 Group Chief Executive Officer C2 2 secondary
54 273 Group Chief Executive Officer X5 3 secondary
54 897 Group Regional Chief Executive Officer X6 4 secondary
54 897 Group Regional Chief Executive Officer F6 5 secondary
54 39 Director VY 6 secondary
54 39 Director G7 7 secondary
Я хочуиметь количество отдельных FunctionId на сотрудника и получить сотрудника, имеющего максимальное количество функций, что в случае EmployeeId = 54 3 различных функции.