Ringkasan fungsi berguna

Ringkasan Statistik dan Window Functions di PostgreSQL

Michel Semaan

Data Scientist

Banyak null

Kueri

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 menandakan total grup

Hasil

| 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    |
Ringkasan Statistik dan Window Functions di PostgreSQL

Memperkenalkan COALESCE

  • COALESCE() mengambil daftar nilai dan mengembalikan nilai pertama yang bukan null, dari kiri ke kanan
  • COALESCE(null, null, 1, null, 2) → 1
  • Berguna saat operasi SQL mengembalikan null
    • ROLLUP dan CUBE
    • Pivot
    • LAG dan LEAD
Ringkasan Statistik dan Window Functions di PostgreSQL

Menghilangkan null

Kueri

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;

Hasil

| 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     |
Ringkasan Statistik dan Window Functions di PostgreSQL

Memadatkan data

Sebelum

| Country | Rank |
|---------|------|
| CHN     | 1    |
| RUS     | 2    |
| USA     | 3    |
  • Rank berlebih karena peringkat tersirat

Sesudah

CHN, RUS, USA
  • Ringkas dan tetap lengkap karena peringkat tersirat
Ringkasan Statistik dan Window Functions di PostgreSQL

Memperkenalkan STRING_AGG

  • STRING_AGG(column, separator) mengambil semua nilai kolom dan menggabungkannya, dengan separator di antara setiap nilai

STRING_AGG(Letter, ', ') mengubah ini...

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

...menjadi ini

A, B, C
Ringkasan Statistik dan Window Functions di PostgreSQL

Kueri dan hasil

Sebelum

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;

Sesudah

WITH Country_Medals AS (...),

  Country_Ranks AS (...)

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

Hasil

CHN, RUS, USA
Ringkasan Statistik dan Window Functions di PostgreSQL

Ayo berlatih!

Ringkasan Statistik dan Window Functions di PostgreSQL

Preparing Video For Download...