与另一行聚合函数分组
group by with another row aggregate function
我正在尝试从部门列中获取最高薪水,但我也
想认识那个部门的人
我可以在这里做什么?
create table if not exists employee(
id serial unique,
firstName varchar (15),
lastName varchar(15),
department varchar (20),
salary int
);
select department, max(salary) from employee
group by department
计算按薪水降序排序的排名。
前1名将获得第1名。
select department, salary, firstName, lastName
from
(
select department, salary, firstName, lastName
, dense_rank() over (partition by department order by salary desc) as Rnk
from employee
) q
where Rnk = 1
PostgreSQL 有一个漂亮的 distinct on
语法,您可以使用:
SELECT DISTINCT ON (department) *
FROM employee
ORDER BY department ASC, salary DESC
我正在尝试从部门列中获取最高薪水,但我也 想认识那个部门的人
我可以在这里做什么?
create table if not exists employee(
id serial unique,
firstName varchar (15),
lastName varchar(15),
department varchar (20),
salary int
);
select department, max(salary) from employee
group by department
计算按薪水降序排序的排名。
前1名将获得第1名。
select department, salary, firstName, lastName
from
(
select department, salary, firstName, lastName
, dense_rank() over (partition by department order by salary desc) as Rnk
from employee
) q
where Rnk = 1
PostgreSQL 有一个漂亮的 distinct on
语法,您可以使用:
SELECT DISTINCT ON (department) *
FROM employee
ORDER BY department ASC, salary DESC