Еще один способ сделать то же самое, что показал @ MT0 в своем ответе. Мой - скорее комментарий, чтобы немного его изменить, но я не могу комментировать (не хватает очков репутации), поэтому добавление в качестве нового ответа (извините).
with q1 as (
select
employee_id, manager_id,
COUNT(1) OVER ( PARTITION BY manager_id ) AS num_emps_with_same_manager
from employees e
),
q2 as (
select
employee_id, manager_id,
num_emps_with_same_manager,
RANK() OVER ( ORDER BY num_emps_with_same_manager DESC ) AS rnk,
min(manager_id) keep (DENSE_RANK FIRST ORDER BY num_emps_with_same_manager DESC, manager_id ASC) over () as most_pop_mgr_asc_id
from q1
)
select *
from q2
where 1=1
--and rnk = 1
and manager_id = most_pop_mgr_asc_id
order by 1
;
также дает самый популярный мгр.
EMPLOYEE_ID MANAGER_ID NUM_EMPS_WITH_SAME_MANAGER RNK MOST_POP_MGR_ASC_ID
101 100 4 1 100
102 100 4 1 100
124 100 4 1 100
149 100 4 1 100
Еще один способ сделать то же самое, но сделать его более интересным / вовлеченным, - это получить список сотрудников, принадлежащих к наиболее популярному менеджеру, а также другого менеджера. (выбран 3-й по популярности менеджер).
with q1 as (
select
employee_id, manager_id, COUNT(1) OVER ( PARTITION BY manager_id ) AS num_emps_with_same_manager
from employees e
),
q2 as (
select
employee_id, manager_id, num_emps_with_same_manager,
DENSE_RANK() OVER ( ORDER BY num_emps_with_same_manager DESC ) AS rnk,
min(manager_id) keep (DENSE_RANK FIRST ORDER BY num_emps_with_same_manager DESC, manager_id ASC) over () as most_pop_mgr_asc_id,
min(num_emps_with_same_manager) keep (DENSE_RANK FIRST ORDER BY num_emps_with_same_manager DESC, manager_id ASC) over () as most_pop_mgr_num_emps,
listagg(employee_id, ',') WITHIN GROUP (ORDER by employee_id ASC) OVER (PARTITION BY manager_id) as mgr_emps
from q1
),
q3 as (
select
q2.num_emps_with_same_manager as mgr_#emps, q2.manager_id as mgr, q2.rnk, q2.most_pop_mgr_asc_id as pop1_mgr, q2.most_pop_mgr_num_emps as pop1_mgr_#emps,
cast(NTH_VALUE(min(mgr_emps), 1) OVER (ORDER BY num_emps_with_same_manager DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as varchar2(20)) as pop1_mgr_emps,
cast(min(case when rnk = 1 then mgr_emps end) over () as varchar2(20)) as pop1_mgr_emps_alt,
cast(NTH_VALUE(min(manager_id), 3) OVER (ORDER BY num_emps_with_same_manager DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as integer) as pop3_mgr,
cast(NTH_VALUE(min(num_emps_with_same_manager), 3) OVER (ORDER BY num_emps_with_same_manager DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as integer) as pop3_mgr_#emps,
cast(NTH_VALUE(min(mgr_emps), 3) OVER (ORDER BY num_emps_with_same_manager DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as varchar2(20)) as pop3_mgr_emps,
cast(min(case when rnk = 3 then mgr_emps end) over () as varchar2(20)) as pop3_mgr_emps_alt
from q2
where 1=1
--and rnk = 1
--and manager_id = most_pop_mgr_asc_id
group by manager_id, num_emps_with_same_manager, rnk, most_pop_mgr_asc_id, most_pop_mgr_num_emps, case when rnk = 1 then mgr_emps end, case when rnk = 3 then mgr_emps end
order by 1 desc
)
select *
from q3
where rnk = 1
;
т
MGR_#EMPS MGR RNK POP1_MGR POP1_MGR_#EMPS POP1_MGR_EMPS POP1_MGR_EMPS_ALT POP3_MGR POP3_MGR_#EMPS POP3_MGR_EMPS POP3_MGR_EMPS_ALT
---------- ---------- ---------- ---------- -------------- -------------------- -------------------- ---------- -------------- -------------------- --------------------
4 100 1 100 4 101,102,124,149 101,102,124,149 101 2 200,205 200,205