1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Window関数によるシンプルな累計の仕方の模索

Last updated at Posted at 2019-02-25

仕事で累計を求める必要が生じた。さんざん悩んだ結果、簡単にできることを知ったため、忘備録として残す。

ItemCode date siire syukka
A 2019-02-01 16:45:31 100 0
A 2019-02-02 18:02:35 50 150
A 2019-02-03 16:15:00 50 25
A 2019-02-04 18:11:06 25 50

このような表(zaiko_hyou)があったとして

ItemCode date siire syukka zaiko
A 2019-02-01 16:45:31 100 0 100
A 2019-02-02 18:02:35 50 150 0
A 2019-02-03 16:15:00 50 25 25
A 2019-02-04 18:11:06 25 50 0

という表にしたいとき、とりあえずview(v_zaiko_hyou)を作成して当日の在庫増減を算出。

ItemCode date siire syukka toujitu_zaiko
A 2019-02-01 16:45:31 100 0 100
A 2019-02-02 18:02:35 50 150 -100
A 2019-02-03 16:15:00 50 25 25
A 2019-02-04 18:11:06 25 50 -25

作成したview(v_zaiko_hyou)のtoujitu_zaikoに対し、window関数を用いて累計を行うことで在庫数が表現できる。

zaiko.sql
SELECT 
    *,
    SUM(toujitu_zaiko)(PARTITION BY ItemCode ORDER BY date )AS zaiko,
FROM 
    v_zaiko_hyou
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?