[LeetCode] Consecutive Numbers (self join twice)
# Write your MySQL query statement below SELECT DISTINCT l1.Num as ConsecutiveNums FROM Logs l1 JOIN Logs l2 ON l1.Id = l2.Id - 1 JOIN Logs l3 ON l2.Id = l3.Id - 1 WHERE l1.Num = l2.Num AND l2.Num = l3.Num ; self join 을 2번한다. 총 3개의 table이 join한다 결과 {"headers": ["Id", "Num", "Id", "Num", "Id", "Num"], "values": [[1, 1, 2, 1, 3, 1], [2, 1, 3, 1, 4, 2], [3, 1, 4, 2, 5, 1], [4, 2, 5, 1, 6, 2], [5, 1..
2021. 4. 10.
[LeetCode] Reformat Department Table (sum, case, when, then)
SELECT id , SUM(CASE WHEN month = "Jan" THEN revenue END) AS Jan_Revenue , SUM(CASE WHEN month = "Feb" THEN revenue END) AS Feb_Revenue , SUM(CASE WHEN month = "Mar" THEN revenue END) AS Mar_Revenue , SUM(CASE WHEN month = "Apr" THEN revenue END) AS Apr_Revenue , SUM(CASE WHEN month = "May" THEN revenue END) AS May_Revenue , SUM(CASE WHEN month = "Jun" THEN revenue END) AS Jun_Revenue , SUM(CASE..
2021. 4. 9.
[LeetCode] Swap Salary (update, ENUM, SET, CASE, IF)
ENUM data type link ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are sorted in the order you enter them UPDATE salary SET sex = IF(sex='m', 'f', 'm'); UPDATE salary SET sex = CASE sex WHEN '..
2021. 4. 9.