-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3262-FindOverlappingShifts.sql
More file actions
115 lines (108 loc) · 4.52 KB
/
3262-FindOverlappingShifts.sql
File metadata and controls
115 lines (108 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- 3262. Find Overlapping Shifts
-- Table: EmployeeShifts
-- +------------------+---------+
-- | Column Name | Type |
-- +------------------+---------+
-- | employee_id | int |
-- | start_time | time |
-- | end_time | time |
-- +------------------+---------+
-- (employee_id, start_time) is the unique key for this table.
-- This table contains information about the shifts worked by employees, including the start and end times on a specific date.
-- Write a solution to count the number of overlapping shifts for each employee. Two shifts are considered overlapping if one shift’s end_time is later than another shift’s start_time.
-- Return the result table ordered by employee_id in ascending order.
-- The query result format is in the following example.
-- Example:
-- Input:
-- EmployeeShifts table:
-- +-------------+------------+----------+
-- | employee_id | start_time | end_time |
-- +-------------+------------+----------+
-- | 1 | 08:00:00 | 12:00:00 |
-- | 1 | 11:00:00 | 15:00:00 |
-- | 1 | 14:00:00 | 18:00:00 |
-- | 2 | 09:00:00 | 17:00:00 |
-- | 2 | 16:00:00 | 20:00:00 |
-- | 3 | 10:00:00 | 12:00:00 |
-- | 3 | 13:00:00 | 15:00:00 |
-- | 3 | 16:00:00 | 18:00:00 |
-- | 4 | 08:00:00 | 10:00:00 |
-- | 4 | 09:00:00 | 11:00:00 |
-- +-------------+------------+----------+
-- Output:
-- +-------------+--------------------+
-- | employee_id | overlapping_shifts |
-- +-------------+--------------------+
-- | 1 | 2 |
-- | 2 | 1 |
-- | 4 | 1 |
-- +-------------+--------------------+
-- Explanation:
-- Employee 1 has 3 shifts:
-- 08:00:00 to 12:00:00
-- 11:00:00 to 15:00:00
-- 14:00:00 to 18:00:00
-- The first shift overlaps with the second, and the second overlaps with the third, resulting in 2 overlapping shifts.
-- Employee 2 has 2 shifts:
-- 09:00:00 to 17:00:00
-- 16:00:00 to 20:00:00
-- These shifts overlap with each other, resulting in 1 overlapping shift.
-- Employee 3 has 3 shifts:
-- 10:00:00 to 12:00:00
-- 13:00:00 to 15:00:00
-- 16:00:00 to 18:00:00
-- None of these shifts overlap, so Employee 3 is not included in the output.
-- Employee 4 has 2 shifts:
-- 08:00:00 to 10:00:00
-- 09:00:00 to 11:00:00
-- These shifts overlap with each other, resulting in 1 overlapping shift.
-- The output shows the employee_id and the count of overlapping shifts for each employee who has at least one overlapping shift, ordered by employee_id in ascending order.
-- Create table if not exists EmployeeShifts(employee_id int, start_time time, end_time time)
-- Truncate table EmployeeShifts
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('1', '08:00:00', '12:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('1', '11:00:00', '15:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('1', '14:00:00', '18:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('2', '09:00:00', '17:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('2', '16:00:00', '20:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('3', '10:00:00', '12:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('3', '13:00:00', '15:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('3', '16:00:00', '18:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('4', '08:00:00', '10:00:00')
-- insert into EmployeeShifts (employee_id, start_time, end_time) values ('4', '09:00:00', '11:00:00')
-- Write your MySQL query statement below
WITH t1 AS (
SELECT
employee_id,
start_time,
end_time,
ROW_NUMBER() OVER (PARTITION BY employee_id ORDER BY start_time) AS rn
FROM
EmployeeShifts
), t2 AS (
SELECT
e1.employee_id AS e1_id,
e2.employee_id AS e2_id
FROM
t1 AS e1,
t1 As e2
WHERE
e1.employee_id = e2.employee_id AND
e1.rn < e2.rn AND
e1.end_time > e2.start_time
)
-- SELECT * FROM t2
-- | e1_id | e2_id |
-- | ----- | ----- |
-- | 1 | 1 |
-- | 1 | 1 |
-- | 2 | 2 |
-- | 4 | 4 |
SELECT
e1_id AS employee_id,
COUNT(*) AS overlapping_shifts
FROM
t2
GROUP BY
e1_id
ORDER BY
employee_id -- Return the result table ordered by employee_id in ascending order