У меня следующий запрос:
select distinct p.title, e.first_name, e.last_name, max(e.salary)
from employees as e
inner join employees_projects as ep
on e.id = ep.employee_id
inner join projects as p
on p.id = ep.project_id
group by 1,2,3
order by p.title
, который возвращает несколько строк на заголовок. Я хочу только максимальную зарплату за каждый титул.
title | first_name | last_name | max
--------------------------+------------+-----------+-------
Build a cool site | Cailin | Ninson | 30000
Build a cool site | Ian | Peterson | 80000
Build a cool site | Mike | Peterson | 20000
Design 3 New Silly Walks | Ava | Muffinson | 10000
Update TPS Reports | John | Smith | 20000
Подправлен @ ревностный код, и это работает:
select
title,
first_name,
last_name,
salary
from
(select
distinct p.title,
e.first_name,
e.last_name,
e.salary,
dense_rank() over (partition by p.title order by e.salary desc) as rnk
from employees as e
inner join employees_projects as ep
on e.id = ep.employee_id
inner join projects as p
on p.id = ep.project_id
group by 1,2,3, 4
) t
where rnk = 1
order by title