bigquery 中的滚动中位数

Rolling median in bigquery

我在 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 中计算滚动中位数(没有当前行)?

谢谢!

试试下面

select 
  user_id,
  transaction_month,
  rolling_avg_spend,
  (select distinct percentile_cont(spend, 0.5) over() 
   from unnest(rolling_spends) spend
  ) as rolling_median_spend
from (
  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,
    array_agg(spend) over(partition by user_id order by transaction_month rows between unbounded preceding and 1 preceding) as rolling_spends,
  from monthly_spend  
)