Anda di halaman 1dari 3

# PURPOSE

SCOPE & APPLICATION
------------------Everyone dealing with analytic functions LEAD & LAG.
How to Use Lead & Lag Functions
------------------------------Lead and Lag functions, with all the other analytic functions,
starting with 8.1.6 rdbms release do not need the installation
of the Time Series Option because they have been implemented
into SQL.
The LEAD and LAG functions give you the possibility to access
to more than one row of a table at the same time without an
explicit self-join.
Using them you can specify an offset whose default is 1.
In this article, for all the examples, we will use the table
FACT:
SQL> desc facts
Name
--------------EXP
YEAR
MONTH

Null?
Type
-------- ------------NUMBER(10)
NOT NULL NUMBER(4)
NOT NULL NUMBER(2)

with values:
EXP
YEAR
MONTH
---------- ---------- ---------1000000
2000
1
2000000
2000
2
3000000
2000
3
4000000
2000
4
5000000
2000
5
1110000
2001
1
2220000
2001
2
3330000
2001
3
4440000
2001
4
5550000
2001
5
EXAMPLES
1. Basic use of LAG function with offset=1:
SQL> select month, year, exp, lag (exp,1) over
as last_year_exp from facts;

(order by month)

MONTH
YEAR
EXP LAST_YEAR_EXP
---------- ---------- ---------- ------------1
2000
1000000
1
2001
1110000
1000000
2
2000
2000000
1110000
2
2001
2220000
2000000
3
2000
3000000
2220000
3
2001
3330000
3000000
4
2000
4000000
3330000
4
2001
4440000
4000000
5
2000
5000000
4440000
5
2001
5550000
5000000

*
*
*
*
*

10 rows selected.
In this case we ca see the comparison ,about the expenditure column,
between x and x-1 records.
Incidentally in this case we have some useful comparison i.e. between
the expenditures of the first month of 2000 and the first one of 2001,
but we have also some senseless comparisons marked with *
2. Basic use of LEAD function with offset=1:
SQL> select month, year, exp, lead(exp,1) over
as last_year_exp from facts;
MONTH
YEAR
EXP LAST_YEAR_EXP
---------- ---------- ---------- ------------1
2000
1000000
1110000
1
2001
1110000
2000000
2
2000
2000000
2220000
2
2001
2220000
3000000
3
2000
3000000
3330000
3
2001
3330000
4000000
4
2000
4000000
4440000
4
2001
4440000
5000000
5
2000
5000000
5550000
5
2001
5550000

(order by month)

*
*
*
*
*

10 rows selected.
In this case we ca see the comparison ,always about the expenditure
column, between x and x-1 records.
Even in this case we have some useful comparison i.e. between
the expenditures of the first month of 2000 and the first one of 2001,
but we have also some senseless comparisons marked with *
3. Use of LAG function in order of obtaining a set of records like:
- current year
- month
- current year monthly expenditures
- last year monthly expenditures
SQL> select * from ( select year,month, exp, lag (exp,1) over (order
by
month, year) as last_year_exp from facts) where year=2001;

YEAR
MONTH
EXP LAST_YEAR_EXP
---------- ---------- ---------- ------------2001
1
1110000
1000000
2001
2
2220000
2000000
2001
3
3330000
3000000
2001
4
4440000
4000000
2001
5
5550000
5000000
4. Use of LEAD function, with offset equal to 5,
obtaining a set of records like:
- last year
- month
- last year expenditures
- current year expenditures

in order of

## SQL> select year as last_year, month, exp as last_year_exp ,

over (order by year) as curr_year_exp from facts;
LAST_YEAR
MONTH LAST_YEAR_EXP CURR_YEAR_EXP
---------- ---------- ------------- ------------2000
1
1000000
1110000
2000
2
2000000
2220000
2000
3
3000000
3330000
2000
4
4000000
4440000
2000
5
5000000
5550000
2001
1
1110000
0
2001
2
2220000
0
2001
3
3330000
0
2001
4
4440000
0
2001
5
5550000
0
10 rows selected.
In this example we have redundant comparison between the monthly
expenditures of year 2001 and the future ones of year 2002.