SQL 고득점 Kit - GROUP BY

GROUP BY

https://programmers.co.kr/learn/courses/30/parts/17044

고양이와 개는 몇 마리 있을까

SELECT ANIMAL_TYPE, count(ANIMAL_ID) as count
FROM ANIMAL_INS
WHERE ANIMAL_TYPE IN('Cat','Dog')
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE

동명 동물 수 찾기

WITH TEMP_TABLE AS (
SELECT NAME, COUNT(ANIMAL_ID) AS COUNT
FROM ANIMAL_INS
WHERE NAME IS NOT NULL
GROUP BY NAME
ORDER BY NAME)
SELECT *
FROM TEMP_TABLE
WHERE COUNT > 1
SELECT NAME, COUNT(ANIMAL_ID)
FROM ANIMAL_INS
GROUP BY NAME
HAVING COUNT(NAME) > 1
ORDER BY NAME

입양 시각 구하기(1)

SELECT hour(DATETIME) as HOUR, COUNT(ANIMAL_ID) as COUNT
FROM ANIMAL_OUTS
GROUP BY hour(DATETIME)
HAVING HOUR BETWEEN 9 AND 19
ORDER BY HOUR;

입양 시각 구하기(2)

SET @HOUR = -1;
SELECT @HOUR := @HOUR + 1 AS HOUR,
(SELECT COUNT(*)
FROM ANIMAL_OUTS
WHERE HOUR(DATETIME)=@HOUR) AS COUNT
FROM ANIMAL_OUTS
GROUP BY HOUR
HAVING HOUR BETWEEN 0 AND 23;

좋은 웹페이지 즐겨찾기