20/01/12 23:01:37.62 aXgbqZV+.net
こんにちは質問させてください。
20190112, A, 91
20190112, B, 82
20190111, A, 93
…
という感じで[日付,名前, 得点]の3カラムのデータがあり、各行の日付段階での、
その人の過去30日平均得点(人別の得点の移動平均)をできるだけ高速に計算したいのですが、
どのようにすればよいでしょうか?
なお、今はsqliteで、以下のような原始的なプログラムを書いてみましたが、遅くて困っております。
# date, nameの組み合わせを抜き出し
list = cur.execute('select date, name from table').fetchall
# listすべてにfor文で処理
for i in range(len(list)):
date = list[i][0]
name = list[i][1]
# dateから30日前の日付を計算
date30before (計算略)
# nameが一致しているそのデータから過去30日分のスコアを抜き出し
scoreList = cur.execute('select score from table where name == ? and ? < date < ?',(date30before, date)).fetchall
# 抜き出した30日分のスコアを平均
scoreListAve(計算略)
# 元のテーブルのdate, nameが一致する行に書き込み
cur.execute('update table set score30Ave = ? where date = ? and name == ?'),(scoreListAve, date, name))
con.commit() # for後、コミット