Recupero

Statistiche riepilogative e funzioni finestra in PostgreSQL

Michel Semaan

Data Scientist

Le quattro funzioni

Relative

  • LAG(column, n) restituisce il valore di column alla riga n posizioni prima di quella corrente
  • LEAD(column, n) restituisce il valore di column alla riga n posizioni dopo quella corrente

Assolute

  • FIRST_VALUE(column) restituisce il primo valore nella tabella o partizione
  • LAST_VALUE(column) restituisce l’ultimo valore nella tabella o partizione
Statistiche riepilogative e funzioni finestra in PostgreSQL

LEAD

Query

WITH Hosts AS (
  SELECT DISTINCT Year, City
  FROM Summer_Medals)

SELECT
  Year, City,
  LEAD(City, 1) OVER (ORDER BY Year ASC)
    AS Next_City,
  LEAD(City, 2) OVER (ORDER BY Year ASC)
    AS After_Next_City
FROM Hosts
ORDER BY Year ASC;

Risultato

| Year | City      | Next_City | After_Next_City |
|------|-----------|-----------|-----------------|
| 1896 | Athens    | Paris     | St Louis        |
| 1900 | Paris     | St Louis  | London          |
| 1904 | St Louis  | London    | Stockholm       |
| 1908 | London    | Stockholm | Antwerp         |
| 1912 | Stockholm | Antwerp   | Paris           |
| ...  | ...       | ...       | ...             |
Statistiche riepilogative e funzioni finestra in PostgreSQL

FIRST_VALUE e LAST_VALUE

Query

SELECT
  Year, City,
  FIRST_VALUE(City) OVER
    (ORDER BY Year ASC) AS First_City,
  LAST_VALUE(City) OVER (
   ORDER BY Year ASC
   RANGE BETWEEN
     UNBOUNDED PRECEDING AND
     UNBOUNDED FOLLOWING
  ) AS Last_City
FROM Hosts
ORDER BY Year ASC;

Risultato

| Year | City      | First_City | Last_City       |
|------|-----------|------------|-----------------|
| 1896 | Athens    | Athens     | London          |
| 1900 | Paris     | Athens     | London          |
| 1904 | St Louis  | Athens     | London          |
| 1908 | London    | Athens     | London          |
| 1912 | Stockholm | Athens     | London          |
  • Per impostazione predefinita, una finestra inizia dall’inizio della tabella o partizione e termina alla riga corrente
  • La clausola RANGE BETWEEN ... estende la finestra fino alla fine della tabella o partizione
Statistiche riepilogative e funzioni finestra in PostgreSQL

Partizionare con LEAD

  • LEAD(Champion, 1) senza PARTITION BY
| Year | Event        | Champion | Next_Champion |
|------|--------------|----------|---------------|
| 2004 | Discus Throw | LTU      | EST           |
| 2008 | Discus Throw | EST      | GER           |
| 2012 | Discus Throw | GER      | SWE           |
| 2004 | Triple Jump  | SWE      | POR           |
| 2008 | Triple Jump  | POR      | USA           |
| 2012 | Triple Jump  | USA      | null          |
  • LEAD(Champion, 1) con PARTITION BY Event
| Year | Event        | Champion | Next_Champion |
|------|--------------|----------|---------------|
| 2004 | Discus Throw | LTU      | EST           |
| 2008 | Discus Throw | EST      | GER           |
| 2012 | Discus Throw | GER      | null          |
| 2004 | Triple Jump  | SWE      | POR           |
| 2008 | Triple Jump  | POR      | USA           |
| 2012 | Triple Jump  | USA      | null          |
Statistiche riepilogative e funzioni finestra in PostgreSQL

Partizionare con FIRST_VALUE

  • FIRST_VALUE(Champion) senza PARTITION BY Event
| Year | Event        | Champion | First_Champion |
|------|--------------|----------|----------------|
| 2004 | Discus Throw | LTU      | LTU            |
| 2008 | Discus Throw | EST      | LTU            |
| 2012 | Discus Throw | GER      | LTU            |
| 2004 | Triple Jump  | SWE      | LTU            |
| 2008 | Triple Jump  | POR      | LTU            |
| 2012 | Triple Jump  | USA      | LTU            |
  • FIRST_VALUE(Champion) con PARTITION BY Event
| Year | Event        | Champion | First_Champion |
|------|--------------|----------|----------------|
| 2004 | Discus Throw | LTU      | LTU            |
| 2008 | Discus Throw | EST      | LTU            |
| 2012 | Discus Throw | GER      | LTU            |
| 2004 | Triple Jump  | SWE      | SWE            |
| 2008 | Triple Jump  | POR      | SWE            |
| 2012 | Triple Jump  | USA      | SWE            |
Statistiche riepilogative e funzioni finestra in PostgreSQL

Passiamo alla pratica !

Statistiche riepilogative e funzioni finestra in PostgreSQL

Preparing Video For Download...