Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Oracle LEAD function

12 July 2020

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_idPRODUCT_idQTY_id
25/09/2007100020
26/09/2007200015
27/09/200710008
28/09/2007200012
29/09/200720002
30/09/200710004

 

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_idORDER_DATE_idNEXT_ORDER_DATE_id
100025/09/200726/09/2007
200026/09/200727/09/2007
100027/09/200728/09/2007
200028/09/200729/09/2007
200029/09/200730/09/2007
100030/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_idORDER_DATE_idNEXT_ORDER_DATE_id
200026/09/200728/09/2007
200028/09/200729/09/2007
200029/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_idPROD_idQTY_id
10002007/09/252007/09/27
10002007/09/272007/09/30
10002007/09/30NULL
20002007/09/262007/09/28
20002007/09/282007/09/29
20002007/09/29NULL

 

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

 
Tags: , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB 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...

Preamble​​MS 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...

Preamble​​Atom 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...

Preamble​​MongoDB 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...

Preamble​​SQLShell 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...

Preamble​​Writing 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...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM 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....

Preamble​​If 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...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...