REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
Oracle LEAD function
The Oracle/PLSQL LEAD function is an analytical function that allows you to query more than one row in a table, while not having a table to join. Returns values from the next row in the table. To return values from the previous row, try the LAG function.
Oracle/PLSQL syntax of the LEAD function
LEAD ( expression_id [, offset_id [, default_id] ] ).
over ([ query_partition_clause_id ] order_by_clause_id )
Parameters or arguments
- expression_id – an expression that can contain other built-in functions, but cannot contain analytical functions.
- offset_id – optional. It is a physical offset from the current row in the table. If this parameter is not specified, it is 1 by default.
- default_id – optional. This is the value that is returned if offset is outside the table. If this option is not specified, it is Null by default.
- query_partition_clause_id – optional. It is used to divide results into groups based on one or more expressions.
- order_by_clause_id – optional. It is used to organize the data in each section.
The LEAD function returns values from the next table row.
You can use the LEAD function in the following versions of Oracle/PLSQL
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
The LEAD function can be used in Oracle/PLSQL
Let’s consider an example. If we have a table that contains the following data:
ORDER_DATE_id | PRODUCT_id | QTY_id |
---|---|---|
25/09/2007 | 1000 | 20 |
26/09/2007 | 2000 | 15 |
27/09/2007 | 1000 | 8 |
28/09/2007 | 2000 | 12 |
29/09/2007 | 2000 | 2 |
30/09/2007 | 1000 | 4 |
And we will launch the next request:
SELECT prod_id,
order_date_id,
LEAD (order_date_id,1) over (ORDER BY order_date_id) AS next_order_date_id
FROM orders_id;
then we’ll get the next result:
PROD_id | ORDER_DATE_id | NEXT_ORDER_DATE_id |
---|---|---|
1000 | 25/09/2007 | 26/09/2007 |
2000 | 26/09/2007 | 27/09/2007 |
1000 | 27/09/2007 | 28/09/2007 |
2000 | 28/09/2007 | 29/09/2007 |
2000 | 29/09/2007 | 30/09/2007 |
1000 | 30/09/2007 |
Since we used offset_id 1, the query returns the next ORDER_DATE.
If we used offset_id 2 instead, it would return ORDER_DATE_id 2 orders later. If we had used offset_id 3, it would have returned ORDER_DATE_id 3 orders later … and so on.
If we want to know only the orders for this prod_id, we will execute the following SQL query:
SELECT prod_id,
order_date_id,
LEAD (order_date_id,1) over (ORDER BY order_date_id) AS next_order_date_id
FROM orders_id
WHERE prod_id = 2000;
The request will return the following result:
PROD_id | ORDER_DATE_id | NEXT_ORDER_DATE_id |
---|---|---|
2000 | 26/09/2007 | 28/09/2007 |
2000 | 28/09/2007 | 29/09/2007 |
2000 | 29/09/2007 |
In this example, the following ORDER_DATE for prod_id = 2000 will be returned and all other orders will be ignored.
Using partitions
Now let’s look at a more complex example where we use the query parameter partition to return the next order_date_id for each prod_id.
Enter the following SQL statement:
SELECT prod_id,
order_date_id,
LEAD (order_date_id,1) OVER (PARTITION BY prod_id ORDER BY order_date_id) AS next_order_date_id
FROM orders_id;
ORDER_DATE_id | PROD_id | QTY_id |
---|---|---|
1000 | 2007/09/25 | 2007/09/27 |
1000 | 2007/09/27 | 2007/09/30 |
1000 | 2007/09/30 | NULL |
2000 | 2007/09/26 | 2007/09/28 |
2000 | 2007/09/28 | 2007/09/29 |
2000 | 2007/09/29 | NULL |
In this example, the LEAD function will divide the results by prod_id and then sort by order_date_id as specified in PARTITION BY prod_id ORDER BY order_date_id. This means that LEAD will only evaluate order_date_id if prod_id matches the prod_id of the current record. When a new prod_id occurs, the LEAD function restarts its calculations and uses the corresponding section prod_id.
As you can see, the third record in the result set is NULL for next_order_date_id, because this is the last record for the section where prod_id equals 1000 (sorted by order_date_id). This is also true for the 6th record, where prod_id is equal to 2000.
LAG vs LEAD Functions in Oracle Database
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...