[LeetCode] 626. Exchange Seats

bin·2023년 1월 17일
0

626. Exchange Seats

https://leetcode.com/problems/exchange-seats/

Requirement

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.

💡 MOD(a, b) : 나누기 후 나머지를 반환하는 함수

Solution

SELECT CASE WHEN MOD(id, 2) = 0 THEN id - 1 /* 짝수 */
            WHEN MOD(id, 2) = 1 AND id + 1 IN (SELECT id FROM Seat) THEN id + 1 /* 홀수 */
            ELSE id  /* 마지막 행 홀수 */
        END id, 
        student
FROM Seat
ORDER BY id ASC

0개의 댓글