ORDER BY 절로 SORT 한 후 1번인 값 가져오기!
with test_table as
(select 1 line_no
,sysdate - 10 t_date
from dual
union all
select 2 line_no
,sysdate - 1 t_date
from dual
union all
select 3 line_no
,sysdate - 2 t_date
from dual
union all
select 4 line_no
,sysdate - 3 t_date
from dual
union all
select 5 line_no
,sysdate - 3 t_date
from dual
union all
select 6 line_no
,sysdate - 4 t_date
from dual
union all
select 7 line_no
,sysdate - 1 t_date
from dual)
select a.*
from (select t.line_no
,t.t_date
,rank() over(order by t_date desc) rn1
,dense_rank() over(order by t_date desc) rn2
,row_number() over(order by t_date desc, line_no asc) rn3
from test_table t
where 1 = 1) a
where 1 = 1
and a.rn3 = 7;
| LINE_NO | T_DATE | RN1 | RN2 | RN3 |
| 2 | 2024-08-21 10:01:37 AM | 1 | 1 | 1 |
| 7 | 2024-08-21 10:01:37 AM | 1 | 1 | 2 |
| 3 | 2024-08-20 10:01:37 AM | 3 | 2 | 3 |
| 4 | 2024-08-19 10:01:37 AM | 4 | 3 | 4 |
| 5 | 2024-08-19 10:01:37 AM | 4 | 3 | 5 |
| 6 | 2024-08-18 10:01:37 AM | 6 | 4 | 6 |
| 1 | 2024-08-12 10:01:37 AM | 7 | 5 | 7 |
'ㄱWORK, ETC > Oracle' 카테고리의 다른 글
| PURGE로 테이블 완전 삭제하기(10g이상) (1) | 2024.11.27 |
|---|---|
| 날짜함수 (0) | 2024.10.17 |
| PL/SQL Developer 자동완성 해지합시다, 느려느려 (0) | 2024.08.13 |
| set_item_instance_property (1) | 2024.01.19 |
| KEY-LISTVAL 과거일자 선택할 수 없게 (1) | 2024.01.09 |