Elementi top con funzioni finestra

Analisi delle serie temporali in PostgreSQL

Jasmin Ludolf

Content Developer, DataCamp

Descrizione tabella

  • Tabella temperatures_monthly
  • Campi:
    • station_id - id stazione meteo
    • year_month - anno e mese
    • t_monthly_min - minima mensile in Celsius
    • t_monthly_max - massima mensile in Celsius
    • t_monthly_avg - media mensile in Celsius
Analisi delle serie temporali in PostgreSQL

Aggregazione tradizionale

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;
Analisi delle serie temporali in PostgreSQL

Aggregazione tradizionale

|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|

Problemi:

  • Manca la stazione 5, non ha dati per il 2018
  • Due righe per la stazione 3, due righe hanno la stessa minima 3
  • Restituisce solo il mese più freddo; e se volessimo i due più freddi?
Analisi delle serie temporali in PostgreSQL

Uso di row_number

  • Dobbiamo numerare ogni riga per stazione
  • ROW_NUMBER() numera le righe per partizione, in ordine crescente

 

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;
Analisi delle serie temporali in PostgreSQL

Numerare le righe

|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|
  • La numerazione riparte con la stazione 2
  • Perché abbiamo partizionato per station_id
Analisi delle serie temporali in PostgreSQL

Temperature più basse

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;
Analisi delle serie temporali in PostgreSQL

Temperature più basse

|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|
Analisi delle serie temporali in PostgreSQL

Valori più alti

  • Per invertire l’ordine, aggiungi DESC:
ROW_NUMBER() OVER (PARTITION BY station_id ORDER BY t_monthly_min DESC)
Analisi delle serie temporali in PostgreSQL

Ayo berlatih!

Analisi delle serie temporali in PostgreSQL

Preparing Video For Download...