오라클 쿼리를 잘몰라서 도움을 구하고자 질문드립니다.
어떻게해야 원하는 결과의 쿼리가 나오는지 알려주시면 감사하겠습니다.
SELECT
ADVICE0_PATIENT_SEQ ,
ADVICE0_YEAR ,
TO_CHAR(ADVICE0_INSERT_DATE, 'YYYYMMDD') AS ADVICE0_INSERT_DATE
FROM
PLAN_ADVICE0
현재 select시 나오는 결과 값
ADVICE0_PATIENT_SEQ ADVICE0_YEAR ADVICE0_INSERT_DATE
1723 2018 20180126
1723 2017 20161101
1725 2018 20180113
1725 2017 20161101
1726 2018 20180113
1726 2017 20161101
1727 2018 20180113
1727 2017 20161101
1761 2017 20161101
1762 2017 20161101
select시 원하는 결과 값
ADVICE0_PATIENT_SEQ ADVICE0_YEAR ADVICE0_INSERT_DATE
1723 2018 20180126
1725 2018 20180113
1726 2018 20180113
1727 2018 20180113
1761 2017 20161101
1762 2017 20161101
원하는 결과의 쿼리는 중복되는 seq가 제일 최신년도에 값으로 출력되길 원합니다.
WITH PLAN_ADVICE0 ( ADVICE0_PATIENT_SEQ , ADVICE0_YEAR , ADVICE0_INSERT_DATE) AS ( SELECT 1723 , '2018' , TO_DATE('20180126','YYYYMMDD') FROM DUAL UNION ALL SELECT 1723 , '2017' , TO_DATE('20161101','YYYYMMDD') FROM DUAL UNION ALL SELECT 1725 , '2018' , TO_DATE('20180113','YYYYMMDD') FROM DUAL UNION ALL SELECT 1725 , '2017' , TO_DATE('20161101','YYYYMMDD') FROM DUAL UNION ALL SELECT 1726 , '2018' , TO_DATE('20180113','YYYYMMDD') FROM DUAL UNION ALL SELECT 1726 , '2017' , TO_DATE('20161101','YYYYMMDD') FROM DUAL UNION ALL SELECT 1727 , '2018' , TO_DATE('20180113','YYYYMMDD') FROM DUAL UNION ALL SELECT 1727 , '2017' , TO_DATE('20161101','YYYYMMDD') FROM DUAL UNION ALL SELECT 1761 , '2017' , TO_DATE('20161101','YYYYMMDD') FROM DUAL UNION ALL SELECT 1762 , '2017' , TO_DATE('20161101','YYYYMMDD') FROM DUAL ) SELECT ADVICE0_PATIENT_SEQ , ADVICE0_YEAR , ADVICE0_INSERT_DATE FROM (SELECT ADVICE0_PATIENT_SEQ , ADVICE0_YEAR , TO_CHAR(ADVICE0_INSERT_DATE, 'YYYYMMDD') AS ADVICE0_INSERT_DATE , ROW_NUMBER() OVER(PARTITION BY ADVICE0_PATIENT_SEQ ORDER BY ADVICE0_YEAR DESC ,ADVICE0_INSERT_DATE DESC ) RN FROM PLAN_ADVICE0 ) WHERE RN = 1 ORDER BY ADVICE0_PATIENT_SEQ