【问题标题】:Rolling median in bigquerybigquery 中的滚动中位数
【发布时间】: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


    【解决方案1】:

    试试下面

    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  
    )
    

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2020-10-05
      • 2016-08-02
      • 2016-05-12
      • 2011-07-28
      相关资源
      最近更新 更多