martedì 27 luglio 2010

Funzione analitica oracle per ottenere il valore del record precedente

lag (expression) over (order-by-clause)
lag (expression, offset) over (order-by-clause)
lag (expression, offset, default-value) over (order-by-clause)

lag (expression) over (query-partition-clause, order-by-clause)
lag is an Analytical function that can be used to get the value of an attribute of the previous row. If you want to retrieve the value of the next row, use lead instead of lag.
The following example will demonstrate this:
A table is created with two attributes: N and M.
set pages 50
set feedback off

create table lag_exp (
  n number,
  m number
);

insert into lag_exp 
  select rownum, mod(rownum * 19 , 13) 
    from all_objects 
   where rownum < 21;
The following select statement will return n and m and additionally a column with the value of M in the previous row:
select n, m, lag(m,1) over (order by n) "Previous M" from lag_exp;

Nessun commento:

Powered By Blogger