- Products -


Software


NEWS


Special



- Knowledge -


Category × Tag



- Like -

公開
作成日:2022/7/6
更新日:2022/7/6

SQLで移動平均を求める方法

・移動平均は、SQLで簡単に求められる。
・方法は、ウインドウ関数を使って、今のレコードの何個前までを対象として平均を求めるかを指定するだけ。

使い方


・SQLの内容

select 列名指定
  , avg (計算対象の列名) over (
    order by 年月等並び替えに使う列名
    rows between 何個前までを対象とするか指定 preceding and current row) as 移動平均の列名
from
  テーブル名;


・具体例

select 年月
  , avg (売上) over (
    order by 年月
    rows between 11 preceding and current row) as 売上12か月移動平均
from
  売上テーブル;


参考


https://qiita.com/omokawa_yasu/items/abb8624fc57a9f4c427c


Category



Tag




関連記事


{{tmp.name}}

{{article.category}} {{article.title}}