|
| 1 | +/* |
| 2 | +Question 3611. Find Overbooked Employees |
| 3 | +Link: https://leetcode.com/problems/find-overbooked-employees/description/?envType=problem-list-v2&envId=database |
| 4 | +
|
| 5 | +Table: employees |
| 6 | +
|
| 7 | ++---------------+---------+ |
| 8 | +| Column Name | Type | |
| 9 | ++---------------+---------+ |
| 10 | +| employee_id | int | |
| 11 | +| employee_name | varchar | |
| 12 | +| department | varchar | |
| 13 | ++---------------+---------+ |
| 14 | +employee_id is the unique identifier for this table. |
| 15 | +Each row contains information about an employee and their department. |
| 16 | +Table: meetings |
| 17 | +
|
| 18 | ++---------------+---------+ |
| 19 | +| Column Name | Type | |
| 20 | ++---------------+---------+ |
| 21 | +| meeting_id | int | |
| 22 | +| employee_id | int | |
| 23 | +| meeting_date | date | |
| 24 | +| meeting_type | varchar | |
| 25 | +| duration_hours| decimal | |
| 26 | ++---------------+---------+ |
| 27 | +meeting_id is the unique identifier for this table. |
| 28 | +Each row represents a meeting attended by an employee. meeting_type can be 'Team', 'Client', or 'Training'. |
| 29 | +Write a solution to find employees who are meeting-heavy - employees who spend more than 50% of their working time in meetings during any given week. |
| 30 | +
|
| 31 | +Assume a standard work week is 40 hours |
| 32 | +Calculate total meeting hours per employee per week (Monday to Sunday) |
| 33 | +An employee is meeting-heavy if their weekly meeting hours > 20 hours (50% of 40 hours) |
| 34 | +Count how many weeks each employee was meeting-heavy |
| 35 | +Only include employees who were meeting-heavy for at least 2 weeks |
| 36 | +Return the result table ordered by the number of meeting-heavy weeks in descending order, then by employee name in ascending order. |
| 37 | +*/ |
| 38 | + |
| 39 | +WITH weeks_stats AS ( |
| 40 | + SELECT |
| 41 | + employee_id, |
| 42 | + EXTRACT(WEEK FROM meeting_date) AS week, --noqa: RF04 |
| 43 | + SUM(duration_hours) AS meet |
| 44 | + FROM meetings |
| 45 | + GROUP BY employee_id, week |
| 46 | + HAVING SUM(duration_hours) > 20 |
| 47 | +) |
| 48 | + |
| 49 | +SELECT |
| 50 | + e.employee_id, |
| 51 | + e.employee_name, |
| 52 | + e.department, |
| 53 | + COUNT(e.employee_id) AS meeting_heavy_weeks |
| 54 | +FROM weeks_stats AS ws |
| 55 | +LEFT JOIN |
| 56 | + employees AS e |
| 57 | + ON ws.employee_id = e.employee_id |
| 58 | +GROUP BY e.employee_id, e.employee_name, e.department |
| 59 | +HAVING COUNT(e.employee_id) >= 2 |
| 60 | +ORDER BY meeting_heavy_weeks DESC, e.employee_name ASC |
0 commit comments