从BigQuery中的观察日期算起过去7天的总和

时间:2019-07-23 03:37:43

标签: google-bigquery

我有一张表格,我想在该表格上计算从观察日开始的最近7天的收入总和。这是我的桌子-

with temp as 
(
select DATE('2019-06-29') as transaction_date, "x"as id, 0 as revenue
union all
select DATE('2019-06-30') as transaction_date, "x"as id,  80 as revenue
union all
select DATE('2019-07-04') as transaction_date, "x"as id, 64 as revenue
union all
select DATE('2019-07-06') as transaction_date, "x"as id, 64 as revenue
union all
select DATE('2019-07-11') as transaction_date, "x"as id, 75 as revenue
union all
select DATE('2019-07-12') as transaction_date, "x"as id, 0 as revenue
)


select * from temp

我想为每个transaction_date花费最近7天的时间。例如,对于具有transaction_date = 2019-07-12的最后一条记录,我想添加另一列,该列从revenue(到2019-07-12为止)过去7天累计2019-07-05,因此,新的rollup_revenue列的值为0 + 75 + 64 = 139。同样,我需要为每个ID计算所有日期的汇总。

注意-该ID可能会或可能不会每天出现。

我尝试过自我加入,但我无法弄清楚。

2 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

   
#standardSQL
SELECT *, 
  SUM(revenue) OVER(
    PARTITION BY id ORDER BY UNIX_DATE(transaction_date) 
    RANGE BETWEEN 6 PRECEDING AND CURRENT ROW
  ) rollup_revenue 
FROM `project.dataset.temp`

您可以使用问题中的示例数据来测试,玩游戏,如下例所示

#standardSQL
WITH `project.dataset.temp` AS (
  SELECT DATE '2019-06-29' AS transaction_date, 'x' AS id, 0 AS revenue UNION ALL
  SELECT '2019-06-30', 'x', 80 UNION ALL
  SELECT '2019-07-04', 'x', 64 UNION ALL
  SELECT '2019-07-06', 'x', 64 UNION ALL
  SELECT '2019-07-11', 'x', 75 UNION ALL
  SELECT '2019-07-12', 'x', 0
)
SELECT *, 
  SUM(revenue) OVER(
    PARTITION BY id ORDER BY UNIX_DATE(transaction_date) 
    RANGE BETWEEN 6 PRECEDING AND CURRENT ROW
  ) rollup_revenue 
FROM `project.dataset.temp`
-- ORDER BY transaction_date   

有结果

Row transaction_date    id  revenue rollup_revenue   
1   2019-06-29          x   0       0    
2   2019-06-30          x   80      80   
3   2019-07-04          x   64      144  
4   2019-07-06          x   64      208  
5   2019-07-11          x   75      139  
6   2019-07-12          x   0       139   

答案 1 :(得分:0)

一个选项使用相关的子查询来找到滚动总和:

SELECT
    transaction_date,
    revenue,
    (SELECT SUM(t2.revenue) FROM temp t2 WHERE t2.transaction_date
         BETWEEN DATE_SUB(t1.transaction_date, INTERVAL 7 DAY) AND
                                       t1.transaction_date) AS rev_7_days
FROM temp t1
ORDER BY
   transaction_date;