Panoramica di funzioni utili

Statistiche riepilogative e funzioni finestra in PostgreSQL

Michel Semaan

Data Scientist

Attenzione ai null

Query

SELECT
  Country, Medal, COUNT(*) AS Awards
FROM summer_medals
WHERE
  Year = 2008 AND Country IN ('CHN', 'RUS')
GROUP BY ROLLUP(Country, Medal)
ORDER BY Country ASC, Medal ASC;
  • i null indicano totali di gruppo

Risultato

| Country | Medal  | Awards |
|---------|--------|--------|
| CHN     | Bronze | 57     |
| CHN     | Gold   | 74     |
| CHN     | Silver | 53     |
| CHN     | null   | 184    |
| RUS     | Bronze | 56     |
| RUS     | Gold   | 43     |
| RUS     | Silver | 44     |
| RUS     | null   | 143    |
| null    | null   | 327    |
Statistiche riepilogative e funzioni finestra in PostgreSQL

Entra in gioco COALESCE

  • COALESCE() prende una lista di valori e restituisce il primo valore non null, da sinistra a destra
  • COALESCE(null, null, 1, null, 2) → 1
  • Utile con operazioni SQL che restituiscono null
    • ROLLUP e CUBE
    • Pivot
    • LAG e LEAD
Statistiche riepilogative e funzioni finestra in PostgreSQL

Azzerare i null

Query

SELECT
  COALESCE(Country, 'Both countries') AS Country,
  COALESCE(Medal, 'All medals') AS Medal,
  COUNT(*) AS Awards
FROM summer_medals
WHERE
  Year = 2008 AND Country IN ('CHN', 'RUS')
GROUP BY ROLLUP(Country, Medal)
ORDER BY Country ASC, Medal ASC;

Risultato

| Country        | Medal      | Awards |
|----------------|------------|--------|
| Both countries | All medals | 327    |
| CHN            | All medals | 184    |
| CHN            | Bronze     | 57     |
| CHN            | Gold       | 74     |
| CHN            | Silver     | 53     |
| RUS            | All medals | 143    |
| RUS            | Bronze     | 56     |
| RUS            | Gold       | 43     |
| RUS            | Silver     | 44     |
Statistiche riepilogative e funzioni finestra in PostgreSQL

Comprimere i dati

Prima

| Country | Rank |
|---------|------|
| CHN     | 1    |
| RUS     | 2    |
| USA     | 3    |
  • Rank è ridondante perché il ranking è implicito

Dopo

CHN, RUS, USA
  • Conciso e completo: il ranking è implicito
Statistiche riepilogative e funzioni finestra in PostgreSQL

Entra in gioco STRING_AGG

  • STRING_AGG(column, separator) prende tutti i valori di una colonna e li concatena, con separator tra ciascun valore

STRING_AGG(Letter, ', ') trasforma questo...

| Letter |
|--------|
| A      |
| B      |
| C      |

...in questo

A, B, C
Statistiche riepilogative e funzioni finestra in PostgreSQL

Query e risultato

Prima

WITH Country_Medals AS (
  SELECT
    Country, COUNT(*) AS Medals
  FROM Summer_Medals
  WHERE Year = 2012
    AND Country IN ('CHN', 'RUS', 'USA')
    AND Medal = 'Gold'
    AND Sport = 'Gymnastics'
  GROUP BY Country),

  SELECT
    Country,
    RANK() OVER (ORDER BY Medals DESC) AS Rank
  FROM Country_Medals
  ORDER BY Rank ASC;

Dopo

WITH Country_Medals AS (...),

  Country_Ranks AS (...)

  SELECT STRING_AGG(Country, ', ')
  FROM Country_Medals;

Risultato

CHN, RUS, USA
Statistiche riepilogative e funzioni finestra in PostgreSQL

Ayo berlatih!

Statistiche riepilogative e funzioni finestra in PostgreSQL

Preparing Video For Download...