Item teratas dengan window function

Analisis Deret Waktu di PostgreSQL

Jasmin Ludolf

Content Developer, DataCamp

Deskripsi tabel

  • Tabel temperatures_monthly
  • Kolom:
    • station_id - id stasiun cuaca
    • year_month - tahun dan bulan
    • t_monthly_min - suhu minimum bulanan (Celsius)
    • t_monthly_max - suhu maksimum bulanan (Celsius)
    • t_monthly_avg - suhu rata-rata bulanan (Celsius)
Analisis Deret Waktu di PostgreSQL

Agregasi tradisional

SELECT station_id, year_month, t_monthly_min
FROM temperatures_monthly AS tm
JOIN

( SELECT station_id, min(t_monthly_min) AS t_monthly_min FROM temperatures_monthly WHERE year_month BETWEEN '2018-01-01' AND '2018-12-31' GROUP BY station_id ) as p USING(station_id, t_monthly_min) WHERE year_month BETWEEN '2018-01-01' AND '2018-12-31' ORDER BY station_id, t_monthly_min;
Analisis Deret Waktu di PostgreSQL

Agregasi tradisional

|station_id|year_month|t_monthly_min|
|----------|----------|-------------|
|         1|2018-12-01|          5.4|
|         2|2018-12-01|          6.6|
|         3|2018-01-01|          1.6|
|         3|2018-02-01|          1.6|
|         4|2018-02-01|        -19.0|
|         6|2018-01-01|          2.6|
|         8|2018-02-01|        -16.3|

Masalah:

  • Stasiun 5 hilang, tidak ada data 2018
  • Dua baris untuk stasiun 3, ada dua bulan dengan suhu minimum yang sama 3
  • Hanya memberi bulan terdingin tunggal; bagaimana jika ingin dua bulan terdingin?
Analisis Deret Waktu di PostgreSQL

Menggunakan row_number

  • Kita perlu memberi nomor tiap baris per stasiun
  • ROW_NUMBER() : memberi nomor baris per partisi, urut naik

 

SELECT station_id, year_month, t_monthly_min,
    ROW_NUMBER() OVER (PARTITION BY station_id ORDER BY t_monthly_min) AS rank
FROM temperatures_monthly AS tm
WHERE year_month BETWEEN '2018-01-01' AND '2018-12-31'
ORDER BY station_id, t_monthly_min;
Analisis Deret Waktu di PostgreSQL

Memberi nomor baris

|station_id|year_month|t_monthly_min|rank|
|----------|----------|-------------|----|
|         1|2018-12-01|          5.4|   1|
|         1|2018-02-01|          6.2|   2|
|         1|2018-01-01|          7.4|   3|
|         1|2018-11-01|          9.0|   4|
|         1|2018-03-01|         10.6|   5| 
|         1|2018-04-01|         14.7|   6|
|         1|2018-10-01|         16.6|   7|
|         1|2018-05-01|         17.2|   8|
|         1|2018-06-01|         21.9|   9|
|         1|2018-09-01|         24.4|  10|
|         1|2018-07-01|         28.1|  11|
|         1|2018-08-01|         28.1|  12|
|         2|2018-12-01|          6.6|   1|
|         2|2018-01-01|          7.6|   2|
|         2|2018-02-01|          8.1|   3|
  • Penomoran mulai ulang pada stasiun 2
  • Karena kita partisi per station_id
Analisis Deret Waktu di PostgreSQL

Suhu terendah

SELECT * FROM
(
    SELECT station_id, year_month, t_monthly_min,
    ROW_NUMBER() OVER 
          (PARTITION BY station_id ORDER BY t_monthly_min) AS rank
    FROM temperatures_monthly
    WHERE year_month BETWEEN '2018-01-01' AND '2018-12-31'
) AS q
WHERE rank < 3
ORDER BY station_id, rank;
Analisis Deret Waktu di PostgreSQL

Suhu terendah

|station_id|year_month|t_monthly_min|rank|
|----------|----------|-------------|----|
|         1|2018-12-01|          5.4|   1|
|         1|2018-02-01|          6.2|   2|
|         2|2018-12-01|          6.6|   1|
|         2|2018-01-01|          7.6|   2|
|         3|2018-01-01|          1.6|   1|
|         3|2018-02-01|          1.6|   2|
Analisis Deret Waktu di PostgreSQL

Nilai tertinggi

  • Untuk membalik urutan, tambahkan DESC:
ROW_NUMBER() OVER (PARTITION BY station_id ORDER BY t_monthly_min DESC)
Analisis Deret Waktu di PostgreSQL

Ayo berlatih!

Analisis Deret Waktu di PostgreSQL

Preparing Video For Download...