안녕하세요 ?
공부하다가 잘안되서 질문드려봅니다.
WITH TEMP AS
(
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S233' AS CODE FROM DUAL UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S437' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S5348' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S134' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S233' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S437' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S5348' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S134' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S233' AS CODE FROM DUAL
)
SELECT YYMM,SEQNO,SNO,DTL_SEQNO, COUNT(CODE) CNT ,CODE
FROM TEMP
GROUP BY YYMM,SEQNO,SNO,DTL_SEQNO
---실행결과값
YYMM,SEQNO,SNO,DTL_SEQNO ,CODE, CNT
202101| 00373| 01| 001| S233 | 3
202101| 00373| 01| 001| S437 | 1
202101| 00373| 01| 001| S5348 | 2
202101| 00373| 01| 001| S134 | 2
이런식으로 결과가 나올겁니다.
제약사항 : YYMM,SEQNO,SNO,DTL_SEQNO 컬럼 그룹기준으로 CODE값을 카운트 한 값중 제일 큰 건수 순서대로 2건만 나오하고 싶습니다. 만약 중복건수가 있다면 CODE값중 MAX값 1개를 가져오면됩니다.
--최종 결과값 (2건)- 아래처럼요..
고수님들 부탁드립니다.
202101| 00373| 01| 001| S233
202101| 00373| 01| 001| S5348
--ROW_NUMBER() OVER(ORDER BY COUNT(1) DESC) RN 한후 나중에 RN <=2 이렇게 하긴 했는데 다른방법이 없을가요?
WITH TEMP AS
(
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S233' AS CODE FROM DUAL UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S437' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S5348' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S134' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S233' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S437' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S5348' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S134' AS CODE FROM DUAL
UNION ALL
SELECT '202101' AS YYMM, '00373' AS SEQNO , '01' AS SNO ,'002' AS DTL_SEQNO ,'S233' AS CODE FROM DUAL
)
SELECT *
FROM
(
SELECT YYMM,
SEQNO,
SNO,
DTL_SEQNO,
COUNT(CODE) CNT ,
CODE,
ROW_NUMBER() OVER(ORDER BY COUNT(1) DESC) RN
FROM TEMP
GROUP BY YYMM,SEQNO,SNO,DTL_SEQNO,CODE
)
WHERE 1=1
AND RN <= 2
이 방법 말고 다른방법 부탁드려봅니다.
다른 방법을 찾는 특별한 이유가 있는지?
방법상의 문제는 없으니 다른 방법을 찾기보다는 다른 몇가지 문제점을 해결해야 합니다.
샘플을 보면 yymm, seqno, sno, dtl_seqno 가 1가지 밖에 없는데?
실제로도 1가지만 조회되는지?
여러가지 값이 함께 조회된다면?
ROW_NUMBER 함수 사용에 PARTITION BY 구문이 추가되어야 할 것입니다.
또한 예시에도 보이듯이 cnt 가 동률을 이룰 경우 어떻게 처리할지 규칙을 정해야 합니다.
2등이 2건인데 둘 다 출력할 것인지? 둘중 하나를 선택할 것인지?
둘중 하나를 선택한다면 선택 기준은 어떻게 되는지?
기타사항으로 COUNT(code) 는 COUNT(*) 사용하는 것이 좋습니다.