SQL / BIGQUERY在日期中具有GAP的运行平均值

时间:2018-09-22 13:54:19

标签: sql google-bigquery average moving-average rolling-average

我在使用BigQuery / SQL进行移动平均时遇到麻烦,我使用了表'SCORES',在使用用户对数据进行分组时,我需要进行30天移动平均,问题是我的日期不是连续的,例如有差距。

下面是我当前的代码:

SELECT user, date,
      AVG(score) OVER (PARTITION BY user ORDER BY date)
FROM SCORES;

我不知道如何在该行中添加日期限制,或者甚至不可能。

我当前的表格如下所示,但当然会有更多的用户:

user    date    score
AA  13/02/2018  2.00
AA  15/02/2018  3.00
AA  17/02/2018  4.00
AA  01/03/2018  5.00
AA  28/03/2018  6.00

然后我需要它成为这个,

user    date    score   30D Avg
AA  13/02/2018  2.00    2.00
AA  15/02/2018  3.00    2.50
AA  17/02/2018  4.00    3.00
AA  01/03/2018  5.00    3.50
AA  28/03/2018  6.00    5.50

在最后一行中,由于日期的原因,它仅向后测量(向后最多30D)是否有任何方法可以在SQL中实现此功能,或者我要求太多?

2 个答案:

答案 0 :(得分:2)

您要使用range between。为此,您需要一个整数,所以:

select s.*,
       avg(score) over (partition by user
                        order by days
                        range between 29 preceding and current row
                       ) as avg_30day
from (select s.*, date_diff(s.date, date('2000-01-01'), day) as days
      from scores s
     ) s;

date_diff()的替代方法是unix_date()

select s.*,
       avg(score) over (partition by user
                        order by unix_days
                        range between 29 preceding and current row
                       ) as avg_30day
from (select s.*, unix_date(s.date) as unix_days
      from scores s
     ) s;

答案 1 :(得分:0)

以下是用于BigQuery标准SQL

#standardSQL
SELECT *,
  AVG(score) OVER (
    PARTITION BY user 
    ORDER BY UNIX_DATE(PARSE_DATE('%d/%m/%Y', date))
    RANGE BETWEEN 29 PRECEDING AND CURRENT ROW
  ) AS avg_30day 
FROM `project.dataset.scores` 

您可以使用问题中的虚拟数据来测试/玩游戏

#standardSQL
WITH `project.dataset.scores` AS (
  SELECT 'AA' user, '13/02/2018' date, 2.00 score UNION ALL
  SELECT 'AA', '15/02/2018', 3.00 UNION ALL
  SELECT 'AA', '17/02/2018', 4.00 UNION ALL
  SELECT 'AA', '01/03/2018', 5.00 UNION ALL
  SELECT 'AA', '28/03/2018', 6.00 
)
SELECT *,
  AVG(score) OVER (
    PARTITION BY user 
    ORDER BY UNIX_DATE(PARSE_DATE('%d/%m/%Y', date))
    RANGE BETWEEN 29 PRECEDING AND CURRENT ROW
  ) AS avg_30day 
FROM `project.dataset.scores` 

结果

Row user    date        score   avg_30day    
1   AA      13/02/2018  2.0     2.0  
2   AA      15/02/2018  3.0     2.5  
3   AA      17/02/2018  4.0     3.0  
4   AA      01/03/2018  5.0     3.5  
5   AA      28/03/2018  6.0     5.5