프로그래머스의 SQL Kit GROUP BY 문제를 풀다가 생소한 문법을 알게 되었습니다.
입양 시각 구하기(1)같은 경우에는 값이 있는 부분만 출력하기에 쉽게 넘어갔지만 (2)는 처음 보는 것이었습니다.
WITH RECURSIVE TIME AS (
SELECT 0 AS HOUR
UNION ALL
SELECT HOUR + 1
FROM TIME
WHERE HOUR < 23
)
SELECT TIME.HOUR, COUNT(ANI.ANIMAL_ID)
FROM TIME
LEFT JOIN ANIMAL_OUTS AS ANI ON TIME.HOUR = HOUR(ANI.DATETIME)
GROUP BY TIME.HOUR
WITH
CTE1 AS (SELECT a,b FROM test1)
CTE2 AS (SELECT c,d FROM test2)
SELECT b,d FROM CTE1 JOIN CTE2
WHERE CTE1.a = CTE2.c;
WITH cte (col1, col2) AS
(
SELECT 1, 2
UNION ALL
SELECT 3, 4
)
SELECT col1, col2 FROM cte;
WITH cte AS
(
SELECT 1 AS col1, 2 AS col2
UNION ALL
SELECT 3, 4
)
SELECT col1, col2 FROM cte;
WITH RECURSIVE cte (n) AS
(
SELECT 1
UNION ALL
SELECT n + 1 FROM cte WHERE n < 5
)
SELECT * FROM cte;
SELECT ... -- 최초 행 반환 (non recursive)
UNION ALL
SELECT ... -- 추가 행 반환 (recursive)
SELECT ... UNION SELECT ... -- 최초 행 반환 (non recursive)
UNION ALL
SELECT ... UNION SELECT ... -- 추가 행 반환 (recursive)
WITH RECURSIVE cte AS
(
SELECT 1 AS n, 'abc' AS str
UNION ALL
SELECT n + 1, CONCAT(str, str) FROM cte WHERE n < 3
)
SELECT * FROM cte;
WITH RECURSIVE cte AS
(
SELECT 1 AS n, CAST('abc' AS CHAR(20)) AS str
UNION ALL
SELECT n + 1, CONCAT(str, str) FROM cte WHERE n < 3
)
SELECT * FROM cte;