-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathexchange-seats.sql
52 lines (47 loc) · 1.04 KB
/
exchange-seats.sql
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
-- Soluton 1, 251 ms
SELECT
id,
CASE WHEN id % 2 != 0 AND id < (SELECT MAX(id) FROM seat) THEN lead_student
WHEN id % 2 != 0 AND id = (SELECT MAX(id) FROM seat) THEN student
ELSE lag_student
END student
FROM (
SELECT
id,
LAG(student) OVER (ORDER BY id) AS lag_student,
LEAD(student) OVER (ORDER BY id) AS lead_student,
student
FROM
seat
) base
;
-- Solution 2, 601 ms
SELECT
id,
CASE WHEN MOD(id, 2) = 0 THEN prev_student
WHEN next_id IS NOT NULL THEN next_student
ELSE student
END AS student
FROM (
SELECT
id,
student,
LAG(student, 1) OVER(ORDER BY id ASC) prev_student,
LEAD(student, 1) OVER(ORDER BY id ASC) next_student,
LEAD(id, 1) OVER(ORDER BY id ASC) next_id
FROM
Seat
) T
;
-- Solution 3, 590ms
SELECT
CASE WHEN MOD(id, 2) = 0 THEN id - 1
WHEN id < (SELECT MAX(id) FROM Seat) THEN id + 1
ELSE id
END AS id,
student
FROM
Seat
ORDER BY
id ASC
;