SQL하나 질문드려봅니다. 0 4 593

by 바라기짱조아 [DB 기타] [2021.04.29 22:42:55]




안녕하세요 ?

공부하다가 잘안되서 질문드려봅니다.

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 이렇게 하긴 했는데 다른방법이 없을가요?

 

 

 



by 바라기짱조아 [2021.04.29 22:47:20]

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 

이 방법 말고 다른방법 부탁드려봅니다.


by 마농 [2021.04.30 08:24:33]

다른 방법을 찾는 특별한 이유가 있는지?
방법상의 문제는 없으니 다른 방법을 찾기보다는 다른 몇가지 문제점을 해결해야 합니다.
샘플을 보면 yymm, seqno, sno, dtl_seqno 가 1가지 밖에 없는데?
실제로도 1가지만 조회되는지?
여러가지 값이 함께 조회된다면?
ROW_NUMBER 함수 사용에 PARTITION BY 구문이 추가되어야 할 것입니다.
또한 예시에도 보이듯이 cnt 가 동률을 이룰 경우 어떻게 처리할지 규칙을 정해야 합니다.
2등이 2건인데 둘 다 출력할 것인지? 둘중 하나를 선택할 것인지?
둘중 하나를 선택한다면 선택 기준은 어떻게 되는지?
기타사항으로 COUNT(code) 는 COUNT(*) 사용하는 것이 좋습니다.


by 바라기짱조아 [2021.04.30 23:40:23]

마뇽님 감사합니다..보니까 오타가 약간있었네요~437은 2건이네요 ㅠ  row_number()over(order by count(*) desc ,code desc)로했습니다.. 말씀하신대로  중복이되면 code 값에 큰값으로해서 2row만 나오면 됩니다..근데 partition by 를 애기하셨는데 어디에 써야할지 궁금합니다


by 마농 [2021.05.03 09:44:48]

, ROW_NUMBER() OVER(PARTITION BY yymm, seqno, sno, dtl_seqno ORDER BY COUNT(*) DESC, code DESC) rn

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입