Skip to main content

Lag

Definition

The lag method allows you to add the LAG function to the query. The LAG function provides access to a row that comes before the current row at a specified physical offset. In other words, from the current row the LAG function can access data of the previous row, or the row before the previous row, and so on.

Available methods

  • lag(KColumn kColumn): Receives a KColumn or a KTableColumn which will be supplied to the LAG function.
  • lag(KColumn kColumn, int offset): Receives a KColumn or a KTableColumn and an offset which will be supplied to the LAG function.
  • lag(KColumn kColumn, int offset, KBaseColumnCastable defaultValue): Receives a KColumn or a KTableColumn and an offset which will be supplied to the LAG function. Additionally, receives a default value which too will be supplied to the LAG function. Among the possible values of this default value are: KTableColumn, KColumn, Values.

To use this way, you need to import the static functions as follows:

import static com.myzlab.k.KFunction.*;

Example: lag(KColumn)

Java code:

k
.select(
APP_USER.FIRST_NAME,
lag(APP_USER.FIRST_NAME).over(wd())
)
.from(APP_USER)
.multiple();

SQL generated:

SELECT
au.first_name,
LAG(au.first_name) OVER()
FROM app_user au

Parameters:

  • None

Example: lag(KColumn, int)

Java code:

k
.select(
APP_USER.FIRST_NAME,
lag(APP_USER.FIRST_NAME, 2).over(wd())
)
.from(APP_USER)
.multiple();

SQL generated:

SELECT
au.first_name,
LAG(au.first_name, 2) OVER()
FROM app_user au

Parameters:

  • None

Example: lag(KColumn, int, KBaseColumnCastable)

Java code:

k
.select(
APP_USER.FIRST_NAME,
lag(APP_USER.FIRST_NAME, 2, val("No name")).over(wd())
)
.from(APP_USER)
.multiple();

SQL generated:

SELECT
au.first_name,
LAG(au.first_name, 2, ?1) OVER()
FROM app_user au

Parameters:

  • ?1: "No name"