提交 46adbcbf 编写于 作者: M Mars Liu

add window function

上级 b63b2b02
repos:
- repo: git@codechina.csdn.net:csdn/skill_tree_hook.git
rev: 15a330214a4d7c2e0c0e42b5dfc3121a56f4dc23
rev: 9430721405a57a809c0d93170fe307a89d979a4b
hooks:
- id: pre-commit
verbose: true
\ No newline at end of file
# 工资最高的人
现有员工信息表如下:
```postgresql
create table employee
(
id serial primary key,
name text,
dept text,
salary money
);
```
下面哪条查询,可以给出每个部门工资最高的前五个员工的 id, name, dept, salary 四项信息?
## 答案
```postgresql
select id, name, dept, salary
from (select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r
from employee) as t
where r <= 5;
```
## 选项
### 结构错误
```postgresql
select id, name, dept, max(salary) as salary
from employee
group by dept
having id < 6;
```
### 结构错误
```postgresql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary) as salary, dept
from employee
group by dept) as r
on l.dept = r.dept and l.salary = r.salary
where count(r.id) <= 5;
```
### 结构错误
```postgresql
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary, 5) as salary, dept
from employee
group by dept) as r
on l.dept = r.dept and l.salary = r.salary
```
### 结构错误
```postgresql
select id, name, dept, salary, rank() over (partition by dept order by salary desc) as r
from employee
where r <= 5;
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册