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

整年工龄

中等 08-datetime 日期时间 datetimeageanniversary

以固定日期 2026-10-01 为基准,计算每位员工满了多少整周年。

  • 从入职日 hire_date 起,每到一次周年日(同月同日)记满一年;基准日当天恰好是周年日的,算满。例如入职 2020-10-01 满 6 年,入职 2020-10-02 只满 5 年。
  • 不是按“相差的年份数”算:入职 2020-12-31 到 2026-10-01 只满 5 年。
  • 入职当天就是基准日的,为 0 年。
  • 数据保证 hire_date 不晚于基准日;hire_date 为 null 时 years 为 null。

输入

  • employees(emp_id, name, dept_id, manager_id, hire_date, salary, title, email),hire_date 是日期。

输出

列为 emp_id, years(整数),每位员工一行,行顺序不限。

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

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_idInt64yearsInt64
1 1 11
2 2 9
3 3 6
4 4 3
5 5 10
6 6 9
7 7 1
8 8 4
9 9 6
10 10 5
11 11 4
12 12 10
13 13 5
14 14 8
15 15 10
16 16 9
17 17 5
18 18 6
19 19 9
20 20 8
21 21 9
22 22 4
23 23 5
24 24 10
25 25 1
26 26 4
27 27 9
28 28 8
29 29 3
30 30 3
31 31 4
32 32 10
33 33 4
34 34 4
35 35 6
36 36 10
37 37 2
38 38 10
39 39 4
40 40 1
共 40 行