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 LAG function

12 July 2020

Oracle LAG function

Oracle/PLSQL LAG function is an analytical function that allows you to query more than one row in a table, while not having an attached table. This returns values from the previous row in the table. To return values from the next row, try the LEAD function.

Oracle/PLSQL syntax of the LAG function

LAG ( 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, the default is Null.
  • 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 LAG function returns values from the previous row in the table.

You can use the LAG function in the following versions of Oracle/PLSQL

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

LAG function can be used in Oracle/PLSQL

Let’s take a look at an example. If we have a table that contains the following data:

ORDER_DATE_idPROD_idQTY_id
25/09/2007100020
26/09/2007200015
27/09/200710008
28/09/2007200012
29/09/200720002
30/09/200710004

 

And we will fulfill the next request:

select prod_id,
order_date_id,
LAG (order_date_id,1) over (ORDER BY order_date_id) AS prev_order_date_id
from orders_id;

then we’ll get the next result:

ORDER_DATE_idPRODUCT_idQTY_id
100025/09/2007
200026/09/200725/09/2007
100027/09/200726/09/2007
200028/09/200727/09/2007
200029/09/200728/09/2007
100030/09/200729/09/2007

 

Since we used offset = 1, the query returns the previous ORDER_DATE_id.

If we used offset = 2 instead of 1, the request would return ORDER_DATE_id 2 positions earlier. If we used offset = 3, then the request would return ORDER_DATE_id on 3 positions earlier … and so on.

If we only want to get orders for this product_id, we will execute the following SQL query:

SELECT prod_id,
order_date_id,
LAG (order_date_id,1) over (ORDER BY order_date_id) AS prev_order_date_id
FROM orders_id
WHERE prod_id = 2000;

We’ll get the result:

ORDER_DATE_idPRODUCT_idQTY_id
200026/09/2007
200028/09/200726/09/2007
200029/09/200728/09/2007

 

In this example, the query returned ORDER_DATE_id for prod_id = 2000 and ignored all other entries.

Using partition

Now let’s look at a more complex example where we use the partition parameter to return the previous order_date_id for each prod_id.

Enter the following SQL statement:

SELECT prod_id,
order_date_id,
LAG (order_date_id,1) OVER (PARTITION BY prod_id ORDER BY order_date_id) AS prev_order_date_id
FROM orders_id;

It will return the next result:

PRODUCT_idORDER_DATE_idPREV_ORDER_DATE_id
10002007/09/25NULL
10002007/09/272007/09/25
10002007/09/302007/09/27
20002007/09/26NULL
20002007/09/282007/09/26
20002007/09/292007/09/28

 

In this example, the LAG 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 the LAG function will only evaluate the order_date_id value if prod_id matches the prod_id of the current record. When a new prod_id occurs, the LAG function restarts its calculations and uses the corresponding section prod_id.

As you can see, the first record in the result set has the value NULL for prev_order_date_id, because this is the first record for a section where prod_id is 1000 (sorted by order_date_id), so there is no lower order_date_id value. This is also true for the 4th entry, where prod_id is equal to 2000.

LAG and LEAD : Problem Solving using Analytic Functions

 
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...