我有一个简单的表格,下面有很多ID和日期。
ID Date
10R46 2014-11-23
10R46 2016-04-11
100R9 2016-12-21
10R91 2013-05-03
... ...
我想制定一个查询,该查询计算日期滚动时间范围的唯一ID,例如十天。这意味着对于每个日期,它应该给出该日期和10天之后的唯一ID的数量。结果看起来应该是这样的。
UniqueTenDays Date
200 2014-11-23
324 2014-11-24
522 2014-11-25
532 2014-11-26
... ...
下面有些东西但我知道我需要应用WHERE子句并以某种方式计算每个Date的ID。
SELECT Date, COUNT(DISTINCT ID)
FROM T
WHERE Date BETWEEN DATE_SUB(Date, INTERVAL 10 DAY) AND Date
GROUP BY Date
ORDER BY Date
提前致谢。
答案 0 :(得分:2)
以下是BigQuery Standard SQL
#standardSQL
WITH temp1 AS (
SELECT dt, STRING_AGG(DISTINCT id) AS users
FROM `project.dataset.yourtable`
GROUP BY dt
), temp2 AS (
SELECT
dt,
STRING_AGG(users) OVER(ORDER BY UNIX_DATE(dt) RANGE BETWEEN 10 PRECEDING AND CURRENT ROW) users
FROM temp1
)
SELECT dt,
(SELECT COUNT(DISTINCT id) FROM UNNEST(SPLIT(users)) AS id) UniqueTenDays
FROM temp2
您可以使用虚拟数据进行测试/播放,如下所示
#standardSQL
WITH `project.dataset.yourtable` AS (
SELECT '10R46' id, DATE '2014-11-23' dt UNION ALL
SELECT '10R46', DATE '2016-04-11' UNION ALL
SELECT '10R46', DATE '2016-04-12' UNION ALL
SELECT '10R47', DATE '2016-04-13' UNION ALL
SELECT '10R48', DATE '2016-04-14' UNION ALL
SELECT '100R9', DATE '2016-12-21' UNION ALL
SELECT '10R91', DATE '2013-05-03'
), temp1 AS (
SELECT dt, STRING_AGG(DISTINCT id) AS users
FROM `project.dataset.yourtable`
GROUP BY dt
), temp2 AS (
SELECT
dt,
STRING_AGG(users) OVER(ORDER BY UNIX_DATE(dt) RANGE BETWEEN 10 PRECEDING AND CURRENT ROW) users
FROM temp1
)
SELECT dt,
(SELECT COUNT(DISTINCT id) FROM UNNEST(SPLIT(users)) AS id) UniqueTenDays
FROM temp2