by 김재현 [SQL Query] oracle max ROW_NUMBER [2018.07.19 23:04:10]
안녕하세요 현재 ORACLE 11G 사용중입니다.
첨부한 이미지와 같이 MAX로우값을 조인하고 싶습니다.
성적이 같을 수 있는 경우가 있을 수 있기 떄문에 ROW_NUMBER OVER()을 사용하여 짜고 있는데...
select 이름, ROW_NUMBER() over(ORDER BY 성적 DESC) AS 순위 FROM C
을 조인하고 WHERE 순위=1로 하면 제가 원하는 값이 안나오네요..
성적이 없는 경우가 있을 수도 있어 LEFT 조인을 하려고 하니 더욱더 복잡해지네요
답변부탁드립니다...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | -- 새치기 ㅎㅎㅎ -- A TABLE은 굳이 JOIN 안하셔도 됩니다. WITH A AS ( SELECT 'A' AS SCHOOL_CODE FROM DUAL UNION ALL SELECT 'B' AS SCHOOL_CODE FROM DUAL ) , B AS ( SELECT 'A' AS SCHOOL_CODE , 'A1' CLASS_CODE FROM DUAL UNION ALL SELECT 'A' AS SCHOOL_CODE , 'A2' CLASS_CODE FROM DUAL UNION ALL SELECT 'B' AS SCHOOL_CODE , 'B1' CLASS_CODE FROM DUAL ) , C AS ( SELECT 'A1' AS CLASS_CODE , 90 SCORE , '김일' NM , '20180718' DT FROM DUAL UNION ALL SELECT 'A1' AS CLASS_CODE , 90 SCORE , '김DL' NM , '20180716' DT FROM DUAL UNION ALL SELECT 'A1' AS CLASS_CODE , 50 SCORE , '김삼' NM , '20180707' DT FROM DUAL UNION ALL SELECT 'A2' AS CLASS_CODE , 80 SCORE , '김사' NM , '20180707' DT FROM DUAL UNION ALL SELECT 'A2' AS CLASS_CODE , 90 SCORE , '김오' NM , '20180707' DT FROM DUAL UNION ALL SELECT 'B1' AS CLASS_CODE , 40 SCORE , '김육' NM , '20180707' DT FROM DUAL ) SELECT SCHOOL_CODE , CLASS_CODE , NM FROM ( SELECT A.SCHOOL_CODE , A.CLASS_CODE , B.NM , ROW_NUMBER() OVER(PARTITION BY A.CLASS_CODE ORDER BY B.SCORE DESC ) RN FROM B A , C B WHERE A.CLASS_CODE = B.CLASS_CODE AND A.SCHOOL_CODE = 'A' ) WHERE RN = 1 |