BigQuery SQL 将两个日期值之间的差异保持为 7 天

BigQuery SQL to keep the difference between two date values as 7 days

我的BigQuery中有2个日期参数,一个是第一次打开日期(first_open),另一个是取值的日期(date)。我需要找到在特定日期打开的一组用户 (id) 以及他们仅在接下来的 7 天内的值。

例如

1June20(first_open) 用户应该只在 7June20(日期)之前的日期

2June20(first_open) 用户应该只到 8June20(日期)

7June20(first_open) 用户只能到 13June20(日期)

SELECT
  event_name,
  COUNT(DISTINCT id) uniques,
  COUNT(id) as total
FROM
  `x-12.analytics_7.xyz`
WHERE
  (first_open between "2020-06-01" and "2020-06-07") 
  AND (date BETWEEN "20200601" AND "20200613")
  AND event_names in ("app_open","first_open")
  AND platform = "ANDROID"     

GROUP BY
  event_names

正如您从我使用的查询中看到的那样,我将用户的开放时间限制为 7 天,但我无法将他们的值限制为仅 7 天。

根据您的描述,您可以使用 COUNTIF():

SELECT event_name, COUNT(DISTINCT id) uniques, COUNT(id) as total,
       COUNTIF(date <= DATE_ADD(first_open, interval 7 day))
FROM `x-12.analytics_7.xyz`
WHERE first_open between '2020-06-01' and '2020-06-07' and
      date BETWEEN '2020-06-01' AND '2020-06-13- and
      event_names in ('app_open', 'first_open') and
      platform = 'ANDROID'   
GROUP BY event_names;

或者,您可以将逻辑放在 WHERE 子句中:

WHERE first_open between '2020-06-01' and '2020-06-07' and
      date >= first_open and
      date < date_add(first_open, interval 7 day) and
      event_names in ('app_open', 'first_open') and
      platform = 'ANDROID'