【发布时间】:2021-07-21 06:52:28
【问题描述】:
我在 BigQuery 中有一些客户的月度支出数据,结构如下:
CREATE TABLE if not EXISTS monthly_spend (
user_id int,
transaction_month DATE,
spend float
);
INSERT INTO monthly_spend VALUES
(1, '2021-01-01', 0),
(1, '2021-02-01', 1),
(1, '2021-03-01', 1),
(1, '2021-04-01', 2),
(1, '2021-05-01', 5),
(2, '2021-01-01', 5),
(2, '2021-02-01', 0),
(2, '2021-03-01', 1),
(2, '2021-04-01', 2),
(2, '2021-05-01', 2);
我正在尝试使用以下查询计算每月支出的滚动中位数:
select
user_id,
transaction_month,
avg(spend) over(partition by user_id order by transaction_month rows between unbounded preceding and 1 preceding) as rolling_avg_spend,
percentile_cont(spend, 0.5) over(partition by user_id order by transaction_month rows between unbounded preceding and 1 preceding) as rolling_median_spend,
from monthly_spend
但是,我收到以下错误:
Window ORDER BY is not allowed for analytic function percentile_cont at [69:63]
有没有办法在 BigQuery 中计算滚动中位数(没有当前行)?
谢谢!
【问题讨论】:
标签: sql google-bigquery median percentile