我試圖使用LAG函數計算BigQuery中的28天移動總和。用於28天滑動窗口聚合的BigQuery SQL(無需編寫28行SQL)
頂端回答這個問題
Bigquery SQL for sliding window aggregate
從費利佩霍法表示,你可以使用LAG功能。一個例子是:
SELECT
spend + spend_lagged_1day + spend_lagged_2day + spend_lagged_3day + ... + spend_lagged_27day as spend_28_day_sum,
user,
date
FROM (
SELECT spend,
LAG(spend, 1) OVER (PARTITION BY user ORDER BY date) spend_lagged_1day,
LAG(spend, 2) OVER (PARTITION BY user ORDER BY date) spend_lagged_2day,
LAG(spend, 3) OVER (PARTITION BY user ORDER BY date) spend_lagged_3day,
...
LAG(spend, 28) OVER (PARTITION BY user ORDER BY date) spend_lagged_day,
user,
date
FROM user_spend
)
有沒有辦法做到這一點,而不必寫出28行的SQL!