df-drills pandas · Polars · DuckDB SQL · DuckDB Relational 题目列表 登录

密集排名

中等 14-window-rank 窗口:排名 rankdense-ranktiesnull-key

在每个部门内部按工资给员工排名。

  • 部门内按 salary 从高到低排名,最高的名次为 1。
  • 并列的工资名次相同,下一名不跳号(例如工资 100、90、90、80 的名次是 1、2、2、3)。
  • dept_id 为 null 的员工(合同工)视为同一组,组内照样排名。
  • 每个员工都要输出。

输入

  • employees(emp_id, name, dept_id, manager_id, hire_date, salary, title, email)

输出

列为 emp_id, dept_id, salary, rk(rk 为整数),行顺序不限。

输入表 (case base,共 2 个 case:base、ties)

employees hr/employees
#emp_idint64namestringdept_idint64manager_idint64hire_datedatesalaryint64titlestringemailstring
1 1 Grace Liu 1 null 2015-10-01 98000 CEO grace.liu@corp.example
2 2 Leo Wang 2 1 2017-09-09 72000 VP Engineering leo.wang@corp.example
3 3 Sara Chen 3 1 2020-06-05 70000 VP Sales sara.chen@corp.example
4 4 Lucas Zhang 4 2 2023-04-20 52000 Engineering Manager lucas.zhang@corp.example
5 5 Mia Zhao 5 2 2016-07-16 52000 Data Manager mia.zhao@corp.example
6 6 Kevin Wu 3 3 2016-10-23 45000 Sales Manager kevin.wu@corp.example
7 7 Lily Xu 6 4 2025-03-17 48000 Infra Lead lily.xu@corp.example
8 8 Tom Huang 4 4 2022-01-03 46000 Senior Engineer tom.huang@corp.example
9 9 Liam Zhou 4 4 2020-10-01 39000 Software Engineer liam.zhou@corp.example
10 10 Nina Yang 4 4 2020-10-02 39000 Software Engineer nina.yang@corp.example
11 11 Oscar Lin 4 8 2022-07-15 26000 Junior Engineer oscar.lin@corp.example
12 12 Linda Ma 5 5 2016-08-25 41000 Data Engineer linda.ma@corp.example
13 13 Eric Sun 5 5 2021-09-09 33000 Data Analyst eric.sun@corp.example
14 14 Ivy He 5 5 2018-05-29 41000 Data Engineer ivy.he@corp.example
15 15 Lei Gao 5 12 2016-06-02 27000 Junior Data Engineer lei.gao@corp.example
16 16 Hao Guo 6 7 2016-12-18 44000 Site Reliability Engineer hao.guo@corp.example
17 17 Fang Luo 6 7 2020-11-11 38000 Infra engineer fang.luo@corp.example
18 18 Jun Liang 6 16 2020-09-08 25000 Junior Engineer jun.liang@corp.example
19 19 Dana Song 3 6 2016-10-13 31000 Account Executive dana.song@corp.example
20 20 Victor Tang 3 6 2018-09-12 31000 Account Executive victor.tang@corp.example
显示前 20 行,共 40 行

期望输出 (case base,行顺序不限)

#emp_idInt64dept_idInt64salaryInt64rkInt64
1 3 3 70000 1
2 32 3 46500 2
3 6 3 45000 3
4 22 3 36000 4
5 19 3 31000 5
6 20 3 31000 5
7 21 3 29500 6
8 36 3 28000 7
9 40 3 28000 7
10 23 3 12000 8
11 7 6 48000 1
12 16 6 44000 2
13 17 6 38000 3
14 31 6 38000 3
15 18 6 25000 4
16 38 6 25000 4
17 30 4 53000 1
18 4 4 52000 2
19 8 4 46000 3
20 39 4 46000 3
21 9 4 39000 4
22 10 4 39000 4
23 25 4 37000 5
24 35 4 37000 5
25 26 4 30000 6
26 11 4 26000 7
27 1 1 98000 1
28 24 7 61000 1
29 2 2 72000 1
30 33 2 66000 2
31 28 null 24000 1
32 27 null 22000 2
33 5 5 52000 1
34 29 5 47000 2
35 12 5 41000 3
36 14 5 41000 3
37 13 5 33000 4
38 15 5 27000 5
39 34 5 26000 6
40 37 5 23000 7
共 40 行