DENSE_RANK
Examples
select
id,
salary,
dense_rank() over (order by salary)
from employee
order by salary;
id salary dense_rank -- ------ ---------- 3 8.00 1 4 9.00 2 1 10.00 3 5 10.00 3 2 12.00 4
DENSE_RANK
Examplesselect
id,
salary,
dense_rank() over (order by salary)
from employee
order by salary;
id salary dense_rank -- ------ ---------- 3 8.00 1 4 9.00 2 1 10.00 3 5 10.00 3 2 12.00 4
NTILE()
Distributes the rows of the current window partition into the specified number of tiles (groups)
BIGINT
NTILE ( number_of_tiles ) OVER <window_name_or_spec>
Argument | Description |
---|---|
number_of_tiles |
Number of tiles (groups).Restricted to a positive integer literal, a named parameter (PSQL), or a positional parameter (DSQL). |
NTILE
Examplesselect
id,
salary,
rank() over (order by salary),
ntile(3) over (order by salary)
from employee
order by salary;
ID SALARY RANK NTILE == ====== ==== ===== 3 8.00 1 1 4 9.00 2 1 1 10.00 3 2 5 10.00 3 2 2 12.00 5 3
PERCENT_RANK()
Relative rank of a row within a window partition.
DOUBLE PRECISION
PERCENT_RANK () OVER <window_name_or_spec>
PERCENT_RANK
is calculated as the [fblangref50-windowfuncs-rank] minus 1 of the current row divided by the number of rows in the partition minus 1.
In other words, PERCENT_RANK() OVER <window_name_or_spec>
is equivalent to (RANK() OVER <window_name_or_spec> - 1) / CAST(COUNT(*) OVER() - 1 AS DOUBLE PRECISION)
PERCENT_RANK
Examplesselect
id,
salary,
rank() over (order by salary),
percent_rank() over (order by salary)
from employee
order by salary;
id salary rank percent_rank -- ------ ---- ------------ 3 8.00 1 0 4 9.00 2 0.25 1 10.00 3 0.5 5 10.00 3 0.5 2 12.00 5 1
RANK()
Rank of each row in a partition
BIGINT
RANK () OVER <window_name_or_spec>
Rows with the same values of window-order get the same rank with in the partition window-partition, if specified.The rank of a row is equal to the number of rank values in the partition preceding the current row, plus one.
RANK
Examplesselect
id,
salary,
rank() over (order by salary)
from employee
order by salary;
id salary rank -- ------ ---- 3 8.00 1 4 9.00 2 1 10.00 3 5 10.00 3 2 12.00 5
ROW_NUMBER()
Sequential row number in the partition
BIGINT
ROW_NUMBER () OVER <window_name_or_spec>
Returns the sequential row number in the partition, where 1
is the first row in each of the partitions.
ROW_NUMBER
Examplesselect
id,
salary,
row_number() over (order by salary)
from employee
order by salary;
id salary rank -- ------ ---- 3 8.00 1 4 9.00 2 1 10.00 3 5 10.00 4 2 12.00 5
The navigational functions get the simple (non-aggregated) value of an expression from another row of the query, within the same partition.
Important
|
This is likely to produce strange or unexpected results for |
select
id,
salary,
first_value(salary) over (order by salary),
last_value(salary) over (order by salary),
nth_value(salary, 2) over (order by salary),
lag(salary) over (order by salary),
lead(salary) over (order by salary)
from employee
order by salary;
id salary first_value last_value nth_value lag lead -- ------ ----------- ---------- --------- ------ ------ 3 8.00 8.00 8.00 <null> <null> 9.00 4 9.00 8.00 9.00 9.00 8.00 10.00 1 10.00 8.00 10.00 9.00 9.00 10.00 5 10.00 8.00 10.00 9.00 10.00 12.00 2 12.00 8.00 12.00 9.00 10.00 <null>
FIRST_VALUE()
First value of the current partition
The same as type as expr
FIRST_VALUE ( <expr> ) OVER <window_name_or_spec>
Argument | Description |
---|---|
expr |
Expression.May contain a table column, constant, variable, expression, scalar function.Aggregate functions are not allowed as an expression. |
LAG()
Value from row in the current partition with a given offset before the current row
The same as type as expr
LAG ( <expr> [, <offset [, <default>]]) OVER <window_name_or_spec>
Argument | Description |
---|---|
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 |
default |
The default value to return if offset points outside the partition.Default is |
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.
LAG
ExamplesSuppose 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.
select
bydate,
cost,
cost - lag(cost) over (order by bydate) as change,
100 * (cost - lag(cost) over (order by bydate)) /
lag(cost) over (order by bydate) as percent_change
from rate
where bydate between dateadd(-4 day to current_date)
and current_date
order by bydate
bydate cost change percent_change ---------- ------ ------ -------------- 27.10.2014 31.00 <null> <null> 28.10.2014 31.53 0.53 1.7096 29.10.2014 31.40 -0.13 -0.4123 30.10.2014 31.67 0.27 0.8598 31.10.2014 32.00 0.33 1.0419
LAST_VALUE()
Last value from the current partition
The same as type as expr
LAST_VALUE ( <expr> ) OVER <window_name_or_spec>
Argument | Description |
---|---|
expr |
Expression.May contain a table column, constant, variable, expression, scalar function.Aggregate functions are not allowed as an expression. |
See also note on frame for navigational functions.
LEAD()
Value from a row in the current partition with a given offset after the current row
The same as type as expr
LEAD ( <expr> [, <offset [, <default>]]) OVER <window_name_or_spec>
Argument | Description |
---|---|
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 |
default |
The default value to return if offset points outside the partition.Default is |
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.
NTH_VALUE()
The Nth value starting from the first or the last row of the current frame
The same as type as expr
NTH_VALUE ( <expr>, <offset> ) [FROM {FIRST | LAST}] OVER <window_name_or_spec>
Argument | Description |
---|---|
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 ( |
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
.
It is possible to use aggregate functions (but not window functions) inside the OVER
clause.In that case, first the aggregate function is applied to determine the windows, and only then the window functions are applied on those windows.
Note
|
When using aggregate functions inside |
select
code_employee_group,
avg(salary) as avg_salary,
rank() over (order by avg(salary)) as salary_rank
from employee
group by code_employee_group