10.7 Navigational Functions

The navigational functions get the simple (non-aggregated) value of an expression from another row of the query, within the same partition.

Important

FIRST_VALUE, LAST_VALUE and NTH_VALUE also operate on a window frame. For navigational functions, Firebird applies a default frame from the first to the current row of the partition, not to the last. In other words, it behaves as if the following frame is specified:

  1. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

This is likely to produce strange or unexpected results for NTH_VALUE and especially LAST_VALUE.

Example of Navigational Functions
  1. select
  2. id,
  3. salary,
  4. first_value(salary) over (order by salary),
  5. last_value(salary) over (order by salary),
  6. nth_value(salary, 2) over (order by salary),
  7. lag(salary) over (order by salary),
  8. lead(salary) over (order by salary)
  9. from employee
  10. order by salary;

Results

  1. id salary first_value last_value nth_value lag lead
  2. -- ------ ----------- ---------- --------- ------ ------
  3. 3 8.00 8.00 8.00 <null> <null> 9.00
  4. 4 9.00 8.00 9.00 9.00 8.00 10.00
  5. 1 10.00 8.00 10.00 9.00 9.00 10.00
  6. 5 10.00 8.00 10.00 9.00 10.00 12.00
  7. 2 12.00 8.00 12.00 9.00 10.00 <null>

10.7.1 FIRST_VALUE()

Available inDSQL, PSQL

Result typeThe same as type as expr

Syntax

  1. FIRST_VALUE ( <expr> ) OVER <window_name_or_spec>

Table 10.7.1.1 Arguments of FIRST_VALUE

ArgumentDescription

expr

Expression. May contain a table column, constant, variable, expression, scalar function. Aggregate functions are not allowed as an expression.

Returns the first value from the current partition.

See alsoSection 10.7.3, LAST_VALUE(), Section 10.7.5, NTH_VALUE()

10.7.2 LAG()

Available inDSQL, PSQL

Result typeThe same as type as expr

Syntax

  1. LAG ( <expr> [, <offset [, <default>]])
  2. OVER <window_name_or_spec>

Table 10.7.2.1 Arguments of LAG

ArgumentDescription

expr

Expression. May contain a table column, constant, variable, expression, scalar function. Aggregate functions are not allowed as an expression.

offset

The offset in rows before the current row to get the value identified by expr. If offset is not specified, the default is 1. offset can be a column, subquery or other expression that results in a positive integer value, or another type that can be implicitly converted to BIGINT. offset cannot be negative (use LEAD instead).

default

The default value to return if offset points outside the partition. Default is NULL.

The LAG function provides access to the row in the current partition with a given offset before the current row.

If offset points outside the current partition, default will be returned, or NULL if no default was specified.

10.7.2.1 LAG Examples

Suppose you have RATE table that stores the exchange rate for each day. To trace the change of the exchange rate over the past five days you can use the following query.

  1. select
  2. bydate,
  3. cost,
  4. cost - lag(cost) over (order by bydate) as change,
  5. 100 * (cost - lag(cost) over (order by bydate)) /
  6. lag(cost) over (order by bydate) as percent_change
  7. from rate
  8. where bydate between dateadd(-4 day to current_date)
  9. and current_date
  10. order by bydate

Result

  1. bydate cost change percent_change
  2. ---------- ------ ------ --------------
  3. 27.10.2014 31.00 <null> <null>
  4. 28.10.2014 31.53 0.53 1.7096
  5. 29.10.2014 31.40 -0.13 -0.4123
  6. 30.10.2014 31.67 0.27 0.8598
  7. 31.10.2014 32.00 0.33 1.0419

See alsoSection 10.7.4, LEAD()

10.7.3 LAST_VALUE()

Available inDSQL, PSQL

Result typeThe same as type as expr

Syntax

  1. LAST_VALUE ( <expr> ) OVER <window_name_or_spec>

Table 10.7.3.1 Arguments of LAST_VALUE

ArgumentDescription

expr

Expression. May contain a table column, constant, variable, expression, scalar function. Aggregate functions are not allowed as an expression.

Returns the last value from the current partition.

See also note on frame for navigational functions.

See alsoSection 10.7.1, FIRST_VALUE(), Section 10.7.5, NTH_VALUE()

10.7.4 LEAD()

Available inDSQL, PSQL

Result typeThe same as type as expr

Syntax

  1. LEAD ( <expr> [, <offset [, <default>]])
  2. OVER <window_name_or_spec>

Table 10.7.4.1 Arguments of LEAD

ArgumentDescription

expr

Expression. May contain a table column, constant, variable, expression, scalar function. Aggregate functions are not allowed as an expression.

offset

The offset in rows after the current row to get the value identified by expr. If offset is not specified, the default is 1. offset can be a column, subquery or other expression that results in a positive integer value, or another type that can be implicitly converted to BIGINT. offset cannot be negative (use LAG instead).

default

The default value to return if offset points outside the partition. Default is NULL.

The LEAD function provides access to the row in the current partition with a given offset after the current row.

If offset points outside the current partition, default will be returned, or NULL if no default was specified.

See alsoSection 10.7.2, LAG()

10.7.5 NTH_VALUE()

Available inDSQL, PSQL

Result typeThe same as type as expr

Syntax

  1. NTH_VALUE ( <expr>, <offset> )
  2. [FROM {FIRST | LAST}]
  3. OVER <window_name_or_spec>

Table 10.7.5.1 Arguments of NTH_VALUE

ArgumentDescription

expr

Expression. May contain a table column, constant, variable, expression, scalar function. Aggregate functions are not allowed as an expression.

offset

The offset in rows from the start (FROM FIRST), or the last (FROM LAST) to get the value identified by expr. offset can be a column, subquery or other expression that results in a positive integer value, or another type that can be implicitly converted to BIGINT. offset cannot be zero or negative.

The NTH_VALUE function returns the Nth value starting from the first (FROM FIRST) or the last (FROM LAST) row of the current frame, see also note on frame for navigational functions. Offset 1 with FROM FIRST is equivalent to FIRST_VALUE, and offset 1 with FROM LAST is equivalent to LAST_VALUE.

See alsoSection 10.7.1, FIRST_VALUE(), Section 10.7.3, LAST_VALUE()