DS's『 행복합시다 』

Carpe Programming/oracle

오라클 lag() lead() 함수

nolite 2009. 10. 20. 11:45
반응형

select classId
     , avgPoints
     , lag(avgPoints,  1) over (order by classId) prevAvg
     , lead(avgPoints, 1) over (order by classId) nextAvg
  from (
        select '2-1' classId
             , 98.3  avgPoints
          from dual
        union all 
        select '2-2' classId
             , 72.5  avgPoints
          from dual
        union all   
        select '2-3' classId
             , 58.9  avgPoints
          from dual
        union all    
        select '2-4' classId
             , 88.2  avgPoints
          from dual
      )

 

//-------------조회결과----------------

CLASSID AVGPOINTS PREVAVG NEXTAVG
     2-1            98.3                        72.5
     2-2            72.5          98.3         58.9
     2-3            58.9          72.5         88.2
     2-4            88.2          58.9 

 

lag(avgPoints,  1) over (order by classId) prevAvg 

//커서를 뒤로 이동하여 조회하고자 하는 값 avgPoints, 이동되는 수 1, 정렬기준 classId

 

lead(avgPoints, 1) over (order by classId) nextAvg

//커서를 앞으로 이동하여 조회하고자 하는 값 avgPoints, 이동되는 수 1, 정렬기준 classId

 


728x90
반응형