Cadres

Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

Michel Semaan

Data Scientist

Motivation

LAST_VALUE

LAST_VALUE(City) OVER (
 ORDER BY Year ASC
 RANGE BETWEEN
   UNBOUNDED PRECEDING AND
   UNBOUNDED FOLLOWING
) AS Last_City
  • Cadre : RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  • Sans le cadre, LAST_VALUE renverrait la valeur de la ligne dans la colonne Ville
  • Par défaut, un cadre commence au début d'un tableau ou d'une partition et se termine à la ligne actuelle
Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

ROWS BETWEEN

  • ROWS BETWEEN [START] AND [FINISH]
    • n PRECEDING : n lignes avant la ligne actuelle
    • CURRENT ROW : la ligne actuelle
    • n FOLLOWING : n lignes après la ligne actuelle

Exemples

  • ROWS BETWEEN 3 PRECEDING AND CURRENT ROW
  • ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
  • ROWS BETWEEN 5 PRECEDING AND 1 PRECEDING
Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

Tableau source

Requête

SELECT
  Year, COUNT(*) AS Medals
FROM Summer_Medals
WHERE
  Country = 'RUS'
  AND Medal = 'Gold'
GROUP BY Year
ORDER BY Year ASC;

Résultat

| Year | Medals |
|------|--------|
| 1996 | 36     |
| 2000 | 66     |
| 2004 | 47     |
| 2008 | 43     |
| 2012 | 47     |
Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

MAX sans cadre

Requête

WITH Russia_Medals AS (...)

SELECT
  Year, Medals,
  MAX(Medals)
    OVER (ORDER BY Year ASC) AS Max_Medals
FROM Russia_Medals
ORDER BY Year ASC;

Résultat

| Year | Medals | Max_Medals |
|------|--------|------------|
| 1996 | 36     | 36         |
| 2000 | 66     | 66         |
| 2004 | 47     | 66         |
| 2008 | 43     | 66         |
| 2012 | 47     | 66         |
Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

MAX avec un cadre

Requête

WITH Russia_Medals AS (...)

SELECT
  Year, Medals,
  MAX(Medals)
    OVER (ORDER BY Year ASC) AS Max_Medals,
  MAX(Medals)
    OVER (ORDER BY Year ASC
          ROWS BETWEEN
          1 PRECEDING AND CURRENT ROW)
    AS Max_Medals_Last
FROM Russia_Medals
ORDER BY Year ASC;

Résultat

| Year | Medals | Max_Medals | Max_Medals_Last |
|------|--------|------------|-----------------|
| 1996 | 36     | 36         | 36              |
| 2000 | 66     | 66         | 66              |
| 2004 | 47     | 66         | 66              |
| 2008 | 43     | 66         | 47              |
| 2012 | 47     | 66         | 47              |
Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

Lignes actuelles et suivantes

Requête

WITH Russia_Medals AS (...)

SELECT
  Year, Medals,
  MAX(Medals)
    OVER (ORDER BY Year ASC
          ROWS BETWEEN
          CURRENT ROW AND 1 FOLLOWING)
    AS Max_Medals_Next
FROM Russia_Medals
ORDER BY Year ASC;

Résultat

| Year | Medals | Max_Medals_Next |
|------|--------|-----------------|
| 1996 | 36     | 66              |
| 2000 | 66     | 66              |
| 2004 | 47     | 47              |
| 2008 | 43     | 47              |
| 2012 | 47     | 47              |
Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

Passons à la pratique !

Statistiques récapitulatives et fonctions de fenêtrage PostgreSQL

Preparing Video For Download...