[퀴즈] 중복 저장된 코드의 갯수를 구하라
아래 테이블로부터 중복된 코드의 갯수를 출력하는 쿼리를 작성하세요.
중복된 코드는 'A'와 'B' 두개가 있으니 결과는 2 입니다.
WITH t AS
(
SELECT 'A' cd FROM dual
UNION ALL SELECT 'A' FROM dual
UNION ALL SELECT 'A' FROM dual
UNION ALL SELECT 'B' FROM dual
UNION ALL SELECT 'B' FROM dual
UNION ALL SELECT 'C' FROM dual
UNION ALL SELECT 'D' FROM dual
)
SELECT * FROM t;
[정답보기] <=== 트리플 클릭
SELECT COUNT(COUNT(*)) cnt
FROM t
GROUP BY cd
HAVING COUNT(*) > 1
;
WITH t AS
(
SELECT 'A' cd FROM dual
UNION ALL SELECT 'A' FROM dual
UNION ALL SELECT 'A' FROM dual
UNION ALL SELECT 'B' FROM dual
UNION ALL SELECT 'B' FROM dual
UNION ALL SELECT 'C' FROM dual
UNION ALL SELECT 'D' FROM dual
)
SELECT COUNT(*) FROM (
SELECT CD,COUNT(*) FROM t
GROUP BY CD
HAVING COUNT(*) > 1)