WITH t AS ( SELECT '1'A,'A'b,'00001'c,'같은내용'd FROM dual UNION SELECT '2','C','00001','같은내용' FROM dual UNION SELECT '1','C','00002','같 은 내 용' FROM dual UNION SELECT '2','B','00002','같 은 내 용' FROM dual UNION SELECT '3','A','00002','같 은 내 용' FROM dual UNION SELECT '1','A','00003','같은 내용' FROM dual UNION SELECT '2','C','00003','같은 내용' FROM dual union select '3','B','00003','같은 내용' from dual ) SELECT * FROM t ; ; --C컬럼 기준으로 a컬럼의 max값을 가져오면서 MAX값인 B의 데이터를 출력하는것입니다. --나오는 출력 결과값은 --C컬럼 A B D -- --00001 2 C 같은내용 --00002 3 A 같 은 내 용 --00003 3 B 같은 내용 --조언좀 부탁드립니다.
WITH t AS ( SELECT '1'A,'A'b,'00001'c,'같은내용'd FROM dual UNION SELECT '2','C','00001','같은내용' FROM dual UNION SELECT '1','C','00002','같 은 내 용' FROM dual UNION SELECT '2','B','00002','같 은 내 용' FROM dual UNION SELECT '3','A','00002','같 은 내 용' FROM dual UNION SELECT '1','A','00003','같은 내용' FROM dual UNION SELECT '2','C','00003','같은 내용' FROM dual union select '3','B','00003','같은 내용' from dual ) SELECT C ,MAX(A) KEEP(DENSE_RANK FIRST ORDER BY C DESC) A ,MAX(B) KEEP(DENSE_RANK FIRST ORDER BY A DESC) B ,MAX(D) KEEP(DENSE_RANK FIRST ORDER BY C DESC) D FROM T GROUP BY C
-- 1. KEEP 을 이용하는 방법 -- -- d 가 c 에 종속적이라면 Group By c,d -- 그렇지 않다면 Group By c 하고 d 도 KEEP 을 이용해 구함. SELECT c , MAX(a) a , MAX(b) KEEP(DENSE_RANK LAST ORDER BY a) b , d FROM t GROUP BY c, d ORDER BY c ; -- 2. Row_Number 를 이용하는 방법 -- SELECT c, a, b, d FROM (SELECT c, a, b, d , ROW_NUMBER() OVER(PARTITION BY c ORDER BY a DESC) rn FROM t ) WHERE rn = 1 ;