[leetcode] 626. Exchange Seats

JIEUN KANG·2021년 8월 4일
0

sample 자체는 매우 간단한 편인데,
막상 구현하자니 Note가 까다로웠다.


select next_id as id
     , student
from (
select id as pre_id
     , case 
        when MOD(ID,2) = 1 AND (id = (select max(id) from seat)) then id
        when MOD(ID,2) = 1 then (id+1)
        when MOD(ID,2) = 0 then (id-1)
    end as next_id
    , student 
from seat
) as a
order by next_id asc

case문을 쓸때 순서에 주의(맨 위에부터 조건이 걸리기 시작!)하고,
가장 last가 되는 값을 처리해주니, 그 뒤에는 쉽게 풀렸다.

profile
가장 보통의 존재

0개의 댓글