Total Pageviews

April 24, 2019

4/24/2019 11:06:00 AM

The Oracle/PLSQL LEAD function is an analytic function that lets you query more than one row in a table at a time without having to join the table to itself. It returns values from the next row in the table.

Example
The LEAD function can be used in Oracle/PLSQL.
Let's look at an example. If we had an orders table that contained the following data:
Ordered DateProduct CodeQTY
2019/01/02100020
26-MAR-2019200015
2007/09/2710008
2019/04/22200012

And we ran the following SQL statement:
SELECT product_id, order_date,
LEAD (order_date,1) OVER (ORDER BY order_date) AS next_order_date
FROM orders;
It would return the following result:
Product CodeOrdered DateNEXT Ordered Date
200026-MAR-20194/26/2019
10002/2/20193/2/2019
20004/26/2019null

In this example, the LEAD function will sort in ascending order all of the order_date values in the orders table and then return the next order_date since we used an offset of 1.
 
Related Posts Plugin for WordPress, Blogger...