我有一个名为answers
的表格,列created_at
和response
,响应为整数0
(对于' no'),{{ 1}}(对于'是'),或1
(对于'不知道')。我希望获得响应值的移动平均值,每天过滤掉2个,只考虑前30天。我知道你可以做2
但这只有在你每天都有数据时才有效,而在我的情况下,一周或更长时间内可能没有数据。
我目前的疑问是:
ROWS BETWEEN 29 AND PRECEDING AND CURRENT ROW
但如果用户在SELECT answers.created_at, answers.response,
AVG(answers.response)
OVER(ORDER BY answers.created_at::date ROWS
BETWEEN 29 PRECEDING AND CURRENT ROW) AS rolling_average
FROM answers
WHERE answers.user_id = 'insert_user_id''
AND (answers.response = 0 OR answers.response = 1)
GROUP BY answers.created_at, answers.response
ORDER BY answers.created_at::date
上回复了1
而在2018-3-30
回复了0
,那么这将返回基于之前行的平均值。我想2018-5-15
2018-5-15
而不是0.5
。如何创建一个查询,该查询仅考虑在过去30天内为滚动平均值创建的响应?
答案 0 :(得分:0)
尝试这样的事情:
created_at | response | rolling_averagte
2018-03-30 | 1 | 1.00000000000000000000
2018-05-15 | 0 | 0.00000000000000000000
numpy.matrix
答案 1 :(得分:0)
自Postgres 11起,您可以执行以下操作:
SELECT created_at,
response,
AVG(response) OVER (ORDER BY created_at
RANGE BETWEEN '29 day' PRECEDING AND current row) AS rolling_average
FROM answers
WHERE user_id = 1
AND response in (0,1)
ORDER BY created_at;