BigQuery最近30天的总和

时间:2018-07-17 08:47:24

标签: sql google-bigquery running-total

我正在尝试在BigQuery的Google商品商店公共数据集中获取以下查询:

  
      
  1. 日期
  2.   
  3. 不同用户数
  4.   
  5. 最近30天内不同用户的运行总和
  6.   

例如(为了简单起见,我在示例中使用了3天):

  date        distinct_users     distinct_users_3days
15/07/2018          8                    15
14/07/2018          2                    12
13/07/2018          5                    20
12/07/2018          5                    15
11/07/2018         10                    10
   ...

这是我当前的SQL代码,它获取前两列,但我不知道如何获取运行总和:

SELECT
  date,
  COUNT(DISTINCT(fullVisitorId)) as daily_active_user
FROM
  `bigquery-public-data.google_analytics_sample.ga_sessions_2017*`
WHERE
  _table_suffix BETWEEN "0101"
  AND "0715"
GROUP BY
  date

感谢您的帮助! :)

2 个答案:

答案 0 :(得分:2)

请尝试以下查询3天(SQL Server 2014)-:

SELECT date,COUNT(DISTINCT(fullVisitorId)) as daily_active_user,sum(COUNT(DISTINCT(fullVisitorId))) over (PARTITION BY null ORDER BY date desc ROWS 
BETWEEN CURRENT ROW AND 2 FOLLOWING) AS distinct_users_3days FROM YOUR_TABLE_NAME WHERE _table_suffix BETWEEN '0101' AND '715' GROUP BY date

30天-:

SELECT 
date,COUNT(DISTINCT(fullVisitorId)) as daily_active_user,
sum(COUNT(DISTINCT(fullVisitorId))) over (PARTITION BY null ORDER BY date desc ROWS 
BETWEEN CURRENT ROW AND 29 FOLLOWING) AS distinct_users_3days 
FROM YOUR_TABLE_NAME 
WHERE _table_suffix 
BETWEEN '0101' AND '715' 
GROUP BY date

答案 1 :(得分:2)

我设法弄清了我的问题的答案,所以我想与其他将来可能遇到此问题的人分享。

SQL代码为:

{{1}}

这将在30天的窗口中提供一列汇总不同用户的信息。