Simple solution using Power(), but how to better handle the clumsy condition for the final person who doesn't need to change seat?


  • 0
    L

    I have the following query which seems elegant up to the point where I need to deal with the last person when odd number of people are present. What I did is in the WHERE clause: OR (P1.id = (SELECT max(id) from seat) AND P2.id = (SELECT max(id) from seat) AND (SELECT max(id) from seat) % 2 = 1)

    Is there any better way to do it? I attempted to save result of (SELECT max(id) from seat) as an alias but syntax does not work. My full query is below:

    SELECT P1.id, P2.student
    FROM seat P1, seat P2
    WHERE (P1.id - P2.id = POWER(-1, P1.id)) OR (P1.id = (SELECT max(id) from seat) AND P2.id = (SELECT max(id) from seat) AND (SELECT max(id) from seat) % 2 = 1)
    ORDER BY P1.id;
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.