bigquery sql将两个日期值之间的差值保持为7天

woobm2wo  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(474)

我的bigquery中有两个日期参数,一个是第一次打开日期(first\u open),另一个是获取值的日期(date)。我需要找到一组用户(id)谁在一个特定的日期和他们的值只开放了7天,而不是更多。
如。
6月20日(首次开放)用户只能在6月20日(日期)之前
2月20日(首次开放)用户只能在6月20日(日期)之前使用
2010年6月7日(首次开放)用户只能在2010年6月13日(日期)前使用

  1. SELECT
  2. event_name,
  3. COUNT(DISTINCT id) uniques,
  4. COUNT(id) as total
  5. FROM
  6. `x-12.analytics_7.xyz`
  7. WHERE
  8. (first_open between "2020-06-01" and "2020-06-07")
  9. AND (date BETWEEN "20200601" AND "20200613")
  10. AND event_names in ("app_open","first_open")
  11. AND platform = "ANDROID"
  12. GROUP BY
  13. event_names

从我使用的查询中可以看到,我将用户的开放时间限制为7天,但不能将其值限制为7天。

brvekthn

brvekthn1#

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

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

或者,你可以把逻辑放在 WHERE 条款:

  1. WHERE first_open between '2020-06-01' and '2020-06-07' and
  2. date >= first_open and
  3. date < date_add(first_open, interval 7 day) and
  4. event_names in ('app_open', 'first_open') and
  5. platform = 'ANDROID'
展开查看全部

相关问题