|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3611\. Find Overbooked Employees |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Table: `employees` |
| 9 | + |
| 10 | + +---------------+---------+ |
| 11 | + | Column Name | Type | |
| 12 | + +---------------+---------+ |
| 13 | + | employee_id | int | |
| 14 | + | employee_name | varchar | |
| 15 | + | department | varchar | |
| 16 | + +---------------+---------+ |
| 17 | + employee_id is the unique identifier for this table. |
| 18 | + Each row contains information about an employee and their department. |
| 19 | + |
| 20 | +Table: `meetings` |
| 21 | + |
| 22 | + +---------------+---------+ |
| 23 | + | Column Name | Type | |
| 24 | + +---------------+---------+ |
| 25 | + | meeting_id | int | |
| 26 | + | employee_id | int | |
| 27 | + | meeting_date | date | |
| 28 | + | meeting_type | varchar | |
| 29 | + | duration_hours| decimal | |
| 30 | + +---------------+---------+ |
| 31 | + meeting_id is the unique identifier for this table. |
| 32 | + Each row represents a meeting attended by an employee. meeting_type can be 'Team', 'Client', or 'Training'. |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | +* Assume a standard work week is `40` **hours** |
| 37 | +* Calculate **total meeting hours** per employee **per week** (**Monday to Sunday**) |
| 38 | +* An employee is meeting-heavy if their weekly meeting hours `>` `20` hours (`50%` of `40` hours) |
| 39 | +* Count how many weeks each employee was meeting-heavy |
| 40 | +* **Only include** employees who were meeting-heavy for **at least** `2` **weeks** |
| 41 | + |
| 42 | +Return _the result table ordered by the number of meeting-heavy weeks in **descending** order, then by employee name in **ascending** order_. |
| 43 | + |
| 44 | +The result format is in the following example. |
| 45 | + |
| 46 | +**Example:** |
| 47 | + |
| 48 | +**Input:** |
| 49 | + |
| 50 | +employees table: |
| 51 | + |
| 52 | + +-------------+----------------+-------------+ |
| 53 | + | employee_id | employee_name | department | |
| 54 | + +-------------+----------------+-------------+ |
| 55 | + | 1 | Alice Johnson | Engineering | |
| 56 | + | 2 | Bob Smith | Marketing | |
| 57 | + | 3 | Carol Davis | Sales | |
| 58 | + | 4 | David Wilson | Engineering | |
| 59 | + | 5 | Emma Brown | HR | |
| 60 | + +-------------+----------------+-------------+ |
| 61 | + |
| 62 | +meetings table: |
| 63 | + |
| 64 | + +------------+-------------+--------------+--------------+----------------+ |
| 65 | + | meeting_id | employee_id | meeting_date | meeting_type | duration_hours | |
| 66 | + +------------+-------------+--------------+--------------+----------------+ |
| 67 | + | 1 | 1 | 2023-06-05 | Team | 8.0 | |
| 68 | + | 2 | 1 | 2023-06-06 | Client | 6.0 | |
| 69 | + | 3 | 1 | 2023-06-07 | Training | 7.0 | |
| 70 | + | 4 | 1 | 2023-06-12 | Team | 12.0 | |
| 71 | + | 5 | 1 | 2023-06-13 | Client | 9.0 | |
| 72 | + | 6 | 2 | 2023-06-05 | Team | 15.0 | |
| 73 | + | 7 | 2 | 2023-06-06 | Client | 8.0 | |
| 74 | + | 8 | 2 | 2023-06-12 | Training | 10.0 | |
| 75 | + | 9 | 3 | 2023-06-05 | Team | 4.0 | |
| 76 | + | 10 | 3 | 2023-06-06 | Client | 3.0 | |
| 77 | + | 11 | 4 | 2023-06-05 | Team | 25.0 | |
| 78 | + | 12 | 4 | 2023-06-19 | Client | 22.0 | |
| 79 | + | 13 | 5 | 2023-06-05 | Training | 2.0 | |
| 80 | + +------------+-------------+--------------+--------------+----------------+ |
| 81 | + |
| 82 | +**Output:** |
| 83 | + |
| 84 | + +-------------+---------------+-------------+---------------------+ |
| 85 | + | employee_id | employee_name | department | meeting_heavy_weeks | |
| 86 | + +-------------+---------------+-------------+---------------------+ |
| 87 | + | 1 | Alice Johnson | Engineering | 2 | |
| 88 | + | 4 | David Wilson | Engineering | 2 | |
| 89 | + +-------------+---------------+-------------+---------------------+ |
| 90 | + |
| 91 | +**Explanation:** |
| 92 | + |
| 93 | +* **Alice Johnson (employee\_id = 1):** |
| 94 | + * Week of June 5-11 (2023-06-05 to 2023-06-11): 8.0 + 6.0 + 7.0 = 21.0 hours (> 20 hours) |
| 95 | + * Week of June 12-18 (2023-06-12 to 2023-06-18): 12.0 + 9.0 = 21.0 hours (> 20 hours) |
| 96 | + * Meeting-heavy for 2 weeks |
| 97 | +* **David Wilson (employee\_id = 4):** |
| 98 | + * Week of June 5-11: 25.0 hours (> 20 hours) |
| 99 | + * Week of June 19-25: 22.0 hours (> 20 hours) |
| 100 | + * Meeting-heavy for 2 weeks |
| 101 | +* **Employees not included:** |
| 102 | + * Bob Smith (employee\_id = 2): Week of June 5-11: 15.0 + 8.0 = 23.0 hours (> 20), Week of June 12-18: 10.0 hours (< 20). Only 1 meeting-heavy week |
| 103 | + * Carol Davis (employee\_id = 3): Week of June 5-11: 4.0 + 3.0 = 7.0 hours (< 20). No meeting-heavy weeks |
| 104 | + * Emma Brown (employee\_id = 5): Week of June 5-11: 2.0 hours (< 20). No meeting-heavy weeks |
| 105 | + |
| 106 | +The result table is ordered by meeting\_heavy\_weeks in descending order, then by employee name in ascending order. |
| 107 | + |
| 108 | +## Solution |
| 109 | + |
| 110 | +```sql |
| 111 | +# Write your MySQL query statement below |
| 112 | +WITH process_1 AS ( |
| 113 | + SELECT |
| 114 | + employee_id, |
| 115 | + SUM(duration_hours) AS duration_total |
| 116 | + FROM |
| 117 | + meetings |
| 118 | + GROUP BY |
| 119 | + employee_id, |
| 120 | + WEEKOFYEAR(meeting_date), |
| 121 | + YEAR(meeting_date) |
| 122 | +) |
| 123 | +SELECT |
| 124 | + p.employee_id, |
| 125 | + e.employee_name, |
| 126 | + e.department, |
| 127 | + COUNT(p.employee_id) AS meeting_heavy_weeks |
| 128 | +FROM |
| 129 | + process_1 p |
| 130 | + INNER JOIN employees e ON p.employee_id = e.employee_id |
| 131 | +WHERE |
| 132 | + duration_total > 20 |
| 133 | +GROUP BY |
| 134 | + p.employee_id, |
| 135 | + e.employee_name, |
| 136 | + e.department |
| 137 | +HAVING |
| 138 | + COUNT(p.employee_id) > 1 |
| 139 | +ORDER BY |
| 140 | + meeting_heavy_weeks DESC, |
| 141 | + employee_name ASC; |
| 142 | +``` |
0 commit comments