Een overzicht van handige functies

PostgreSQL Samenvattingsstatistieken en vensterfuncties

Michel Semaan

Data Scientist

Nulls in zicht

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;
  • null betekent groepstotalen

Resultaat

| 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    |
PostgreSQL Samenvattingsstatistieken en vensterfuncties

Maak kennis met COALESCE

  • COALESCE() neemt een lijst met waarden en geeft de eerste niet-null waarde terug, van links naar rechts
  • COALESCE(null, null, 1, null, 2) → 1
  • Handig bij SQL-bewerkingen die null teruggeven
    • ROLLUP en CUBE
    • Pivoteren
    • LAG en LEAD
PostgreSQL Samenvattingsstatistieken en vensterfuncties

Nulls elimineren

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;

Resultaat

| 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     |
PostgreSQL Samenvattingsstatistieken en vensterfuncties

Data comprimeren

Voor

| Country | Rank |
|---------|------|
| CHN     | 1    |
| RUS     | 2    |
| USA     | 3    |
  • Rank is overbodig omdat de rangorde is geïmpliceerd

Na

CHN, RUS, USA
  • Compact en bevat alle benodigde info omdat de rangorde is geïmpliceerd
PostgreSQL Samenvattingsstatistieken en vensterfuncties

Maak kennis met STRING_AGG

  • STRING_AGG(column, separator) neemt alle waarden uit een kolom en voegt ze samen, met separator tussen de waarden

STRING_AGG(Letter, ', ') zet dit om...

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

...in dit

A, B, C
PostgreSQL Samenvattingsstatistieken en vensterfuncties

Query en resultaat

Voor

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;

Na

WITH Country_Medals AS (...),

  Country_Ranks AS (...)

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

Resultaat

CHN, RUS, USA
PostgreSQL Samenvattingsstatistieken en vensterfuncties

Laten we oefenen!

PostgreSQL Samenvattingsstatistieken en vensterfuncties

Preparing Video For Download...