본문 바로가기
ㄱWORK, ETC/Oracle

분석함수 이용하여 RANK 구하자

by YuyU유유 2024. 8. 22.
728x90
반응형
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

 

728x90
반응형