-
leecode - 626. Exchange SeatsSQL 문제풀이 2022. 7. 8. 16:59
난이도 : Medium
Table: Seat
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | +-------------+---------+ id is the primary key column for this table. Each row of this table indicates the name and the ID of a student. id is a continuous increment.
Write an SQL query to swap the seat id of every two consecutive students. If the number of students is odd, the id of the last student is not swapped.
Return the result table ordered by id in ascending order.
SELECT CASE WHEN MOD(id, 2) = 1 AND id = (SELECT MAX(id) FROM Seat) THEN id WHEN MOD(id, 2) = 0 THEN id - 1 ELSE id + 1 END id , student FROM Seat ORDER BY id
Accepted (9.11%)
SELECT ROW_NUMBER() OVER () id , student FROM Seat ORDER BY IF(MOD(id, 2) = 0, id - 1, id + 1)
Accepted (68.17%)
'SQL 문제풀이' 카테고리의 다른 글
leetcode - 597. Friend Requests I: Overall Acceptance Rate (0) 2022.07.09 leetcode - 614. Second Degree Follower (0) 2022.07.08 leetcode - 1378. Replace Employee ID With The Unique Identifier (0) 2022.07.08 leecode - 1350. Students With Invalid Departments (0) 2022.07.08 leecode - 1327. List the Products Ordered in a Period (0) 2022.07.08