LoginSignup
1
0

More than 3 years have passed since last update.

BigQueryで1行ずつの累積を求めるメモ

Posted at

やりたいこと

日付と時間のデータを使って、1行ずつ累積して合計時間を算出したい。
毎回忘れるのでメモ
スクリーンショット 2020-01-15 16.35.07.png

SQL

SELECT
  date,
  minutes,
  SUM(minutes) OVER(ORDER BY date) AS total_minutes
FROM mytable

ORDER BY dateでdateの順番に上から足してくれる
PARTITION BYしていないので、全部の行を順番に累積

結果

スクリーンショット 2020-01-15 16.35.16.png

おまけ

ORDER BYを指定しないと全ての行の和を計算する

SELECT
  date,
  minutes,
  SUM(minutes) OVER() AS total_minutes
FROM mytable

スクリーンショット 2020-01-15 16.42.51.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0