ウィンドウ関数で上位を取得

PostgreSQLで学ぶ時系列分析

Jasmin Ludolf

Content Developer, DataCamp

テーブルの説明

  • temperatures_monthly テーブル
  • 項目:
    • station_id - 気象観測所ID
    • year_month - 年月
    • t_monthly_min - 月間最低気温(摂氏)
    • t_monthly_max - 月間最高気温(摂氏)
    • t_monthly_avg - 月間平均気温(摂氏)
PostgreSQLで学ぶ時系列分析

従来の集計

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;
PostgreSQLで学ぶ時系列分析

従来の集計

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

問題点:

  • 5が欠落(2018年のデータなし)
  • 3は2行(最小値が同じ月が2つ)
  • 最低の1か月のみ。2か月分欲しい場合は?
PostgreSQLで学ぶ時系列分析

ROW_NUMBERの活用

  • 駅ごとに各行へ番号を付与する必要がある
  • ROW_NUMBER():パーティションごとに昇順で行に番号付け

 

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;
PostgreSQLで学ぶ時系列分析

行に番号を振る

|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|
  • 2で番号付けが再開
  • station_idでパーティション分割したため
PostgreSQLで学ぶ時系列分析

最低気温

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;
PostgreSQLで学ぶ時系列分析

最低気温

|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|
PostgreSQLで学ぶ時系列分析

最大値

  • 並びを逆にするにはDESCを追加:
ROW_NUMBER() OVER (PARTITION BY station_id ORDER BY t_monthly_min DESC)
PostgreSQLで学ぶ時系列分析

Vamos praticar!

PostgreSQLで学ぶ時系列分析

Preparing Video For Download...