안녕하세요.
아래에는 여러개의 숫자가 보입니다.
출력하고자 하는 것은 3개이상 연속된 수의 여부를 파악해서 출력해야 하는 것입니다.
단 집계함수를 써서 해결해야 하구요.
데이터는 아래와 같습니다.
WITH RS AS ( SELECT 201 AS N FROM DUAL UNION ALL SELECT 202 FROM DUAL UNION ALL SELECT 204 FROM DUAL UNION ALL SELECT 205 FROM DUAL UNION ALL SELECT 206 FROM DUAL UNION ALL SELECT 209 FROM DUAL UNION ALL SELECT 210 FROM DUAL UNION ALL SELECT 215 FROM DUAL UNION ALL SELECT 216 FROM DUAL UNION ALL SELECT 217 FROM DUAL ) SELECT COUNT(*) FROM RS GROUP BY 1
결과는 아래와 같습니다.
3개 이상 연속수 여부 | |
존재함 |
WITH RS AS ( SELECT 201 AS N FROM DUAL UNION ALL SELECT 202 FROM DUAL UNION ALL SELECT 204 FROM DUAL UNION ALL SELECT 205 FROM DUAL UNION ALL SELECT 206 FROM DUAL UNION ALL SELECT 209 FROM DUAL UNION ALL SELECT 210 FROM DUAL UNION ALL SELECT 215 FROM DUAL UNION ALL SELECT 216 FROM DUAL UNION ALL SELECT 217 FROM DUAL ) SELECT DECODE (NVL (MAX (cnt), 0), 0, '존재하지않음', '존재함') as "3개이상연속수여부" , COUNT(1) AS "건수" FROM (SELECT COUNT (1) cnt FROM (SELECT ROW_NUMBER () OVER (ORDER BY n) rn, n FROM rs) GROUP BY n - rn) WHERE cnt >= 3
SELECT CASE WHEN (SUM(CASE WHEN COUNT(*) >= 3 THEN 1 ELSE 0 END)) > 0 THEN '존재함' ELSE '존재하지않음' END AS "3개 이상 연속수 여부" FROM ( SELECT N, N-ROW_NUMBER() OVER (ORDER BY N) GP FROM RS ) GROUP BY GP ;
SELECT CASE WHEN (SUM(CASE WHEN COUNT(*) >= 3 THEN 1 ELSE 0 END)) > 0 THEN '존재함' ELSE '존재하지않음' END AS "3개 이상 연속수 여부" FROM (SELECT N FROM RS ORDER BY N) GROUP BY N-ROWNUM ;
-- N기준으로 이미 ORDER BY 가 되었다면... SELECT CASE WHEN (SUM(CASE WHEN COUNT(*) >= 3 THEN 1 ELSE 0 END)) > 0 THEN '존재함' ELSE '존재하지않음' END AS "3개 이상 연속수 여부" FROM RS GROUP BY N-ROWNUM ;