我有一个包含日期,事件和用户的表格。有一个名为'A'的事件。我想知道在Sql Bigquery中事件'A'之前和之后发生特定事件的时间。例如,
User Date Events
123 2018-02-13 X.Y.A
123 2018-02-12 X.Y.B
134 2018-02-10 Y.Z.A
123 2018-02-11 A
123 2018-02-01 X.Y.Z
134 2018-02-05 X.Y.B
134 2018-02-04 A
输出将是这样的
User Event Before After
123 A 1 3
134 A 0 1
我必须计算的事件包含特定的前缀。意味着我必须检查以(X.Y.然后某个事件名称)开头的事件。所以,X.Y.SomeEvent是我必须设置计数器的事件。有什么建议吗?
答案 0 :(得分:2)
下面是BigQuery SQL
#standardSQL
SELECT user, event, before, after
FROM (
SELECT user, event,
COUNTIF(event LIKE 'X.Y.%') OVER(PARTITION BY user ORDER BY dt ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) before,
COUNTIF(event LIKE 'X.Y.%') OVER(PARTITION BY user ORDER BY dt ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) after
FROM `project.dataset.events`
)
WHERE event = 'A'
-- ORDER BY user
您可以在问题中使用虚拟数据进行测试
#standardSQL
WITH `project.dataset.events` AS (
SELECT 123 user, '2018-02-13' dt, 'X.Y.A' event UNION ALL
SELECT 123, '2018-02-12', 'X.Y.B' UNION ALL
SELECT 123, '2018-02-11', 'A' UNION ALL
SELECT 134, '2018-02-10', 'Y.Z.A' UNION ALL
SELECT 134, '2018-02-05', 'X.Y.B' UNION ALL
SELECT 134, '2018-02-04', 'A' UNION ALL
SELECT 123, '2018-02-01', 'X.Y.Z'
)
SELECT user, event, before, after
FROM (
SELECT user, event,
COUNTIF(event LIKE 'X.Y.%') OVER(PARTITION BY user ORDER BY dt ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) before,
COUNTIF(event LIKE 'X.Y.%') OVER(PARTITION BY user ORDER BY dt ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) after
FROM `project.dataset.events`
)
WHERE event = 'A'
ORDER BY user
答案 1 :(得分:1)
用户窗口用于查找" A"发生。然后使用条件聚合来计算事件之前和之后的事件:
select userid,
sum(case when date < a_date and event like 'X.Y%' then 1 else 0 end) as before,
sum(case when date > a_date and event like 'X.Y%' then 1 else 0 end) as before
from (select t.*,
min(case when event = 'A' then date end) over (partition by userid) as a_date
from t
) t
group by userid