Each row of leaves records one day of leave taken by one employee. For each department and calendar year of leave, compute the total number of leave days divided by the number of distinct employees in that department who took leave in that year. Employees who took no leave in a given year do not count toward that year's denominator.
Return department, leave_year and avg_leave_days_per_employee, rounded to 2 decimals.
Input Table(s)
employees
| Column | Type | Meaning |
|---|
| employee_id | INT | Employee identifier (primary key) |
| employee_name | VARCHAR(100) | Employee's name |
| department | VARCHAR(100) | The one department the employee belongs to |
leaves
| Column | Type | Meaning |
|---|
| leave_id | INT | Leave record identifier (primary key) |
| employee_id | INT | Employee who took the leave; matches employees.employee_id |
| leave_date | DATE | The day of leave |
Keys: employees.employee_id and leaves.leave_id are primary keys. leaves.employee_id refers to employees.employee_id.
Output Contract
One row per department and year that has at least one leave record:
| Column | Type | Meaning |
|---|
| department | VARCHAR | Department name |
| leave_year | INT | Calendar year of leave_date |
| avg_leave_days_per_employee | NUMERIC | Leave days in that department and year divided by the number of distinct employees who took them, rounded to 2 decimals |
Order by department ascending, then leave_year ascending. That pair is unique in the output.
Example
employees
| employee_id | employee_name | department |
|---|
| 1 | Ana | Engineering |
| 2 | Ben | Engineering |
| 3 | Cara | Sales |
| 4 | Dev | Sales |
| 5 | Eli | Sales |
| 6 | Fay | Engineering |
leaves
| leave_id | employee_id | leave_date |
|---|
| 1 | 1 | 2023-03-01 |
| 2 | 1 | 2023-03-02 |
| 3 | 2 | 2023-07-10 |
| 4 | 1 | 2024-01-05 |
| 5 | 3 | 2023-12-31 |
| 6 | 3 | 2024-01-02 |
| 7 | 3 | 2024-01-03 |
| 8 | 4 | 2024-02-14 |
| 9 | 5 | 2024-05-20 |
Expected result:
| department | leave_year | avg_leave_days_per_employee |
|---|
| Engineering | 2023 | 1.50 |
| Engineering | 2024 | 1.00 |
| Sales | 2023 | 1.00 |
| Sales | 2024 | 1.33 |
Engineering in 2023 has three leave days (two from Ana, one from Ben) over two employees, so 3 / 2 = 1.50. In 2024 only Ana took leave, so Ben is not in that year's denominator, and Fay never took leave at all. Sales in 2024 has four days over three employees, and 4 / 3 = 1.333 rounds to 1.33. Cara's leave on 2023-12-31 belongs to 2023.
Constraints and Clarifications
-
There are 1 to 10,000 employees and 1 to 100,000 leave records.
-
Every
leave_date
is a valid date. The year is the calendar year of that date.
-
Each employee belongs to exactly one department, so
department
is never NULL.
-
Every row of
leaves
counts as one leave day, even when an employee has two rows on the same date.
-
A leave record whose
employee_id
has no match in
employees
has no department and is left out.
-
A department and year with no leave records does not appear in the output.