Fonctions de navigation
Les fonctions de navigation récupèrent les valeurs simples (non agrégées) d’une expression à partir d’une autre chaîne de requête dans la même section.
Important
|
Les fonctions
Pour cette raison, les résultats des fonctions
|
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>
Variante avec un cadre de Window modifié pour les fonctions LAST_VALUE
et NTH_VALUE
.
SELECT
id,
salary,
FIRST_VALUE(salary) OVER (ORDER BY salary),
LAST_VALUE(salary) OVER w,
NTH_VALUE(salary, 2) OVER w,
LAG(salary) OVER (ORDER BY salary),
LEAD(salary) OVER (ORDER BY salary)
FROM employee
WINDOW
w AS (ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
ORDER BY salary;
id salary first_value last_value nth_value lag lead -- ------ ----------- ---------- --------- ------ ------ 3 8.00 8.00 12.00 9.00 <null> 9.00 4 9.00 8.00 12.00 9.00 8.00 10.00 1 10.00 8.00 12.00 9.00 9.00 10.00 5 10.00 8.00 12.00 9.00 10.00 12.00 2 12.00 8.00 12.00 9.00 10.00 <null>