2 回答
TA贡献1893条经验 获得超10个赞
本cumulativeSum应使用来计算累计总和,而不是一个不相等联接:
WITH visitWithIsChange AS
(select
*,
CASE
WHEN (LAG(location,1,'')
OVER (PARTITION BY user_id ORDER BY timestamp)) = location
THEN 0
ELSE 1
END ischange
FROM `ping_table`
-- I don't now about BigQuery, but why do you need this?
--ORDER BY user_id, timestamp
),
visitsWithcumsum AS (
SELECT
*,
SUM(ischange)
OVER (PARTITION BY user_id
ORDER BY timestamp
ROWS UNBOUNDED PREDECING) AS cumulativeSum
FROM visitWithIsChange
)
SELECT
MIN(timestamp) AS first_ping,
MAX(local_timestamp) AS last_ping,
user_id,
chain_id,
FROM visitsWithcumsum
GROUP BY
user_id,
cumulativeSum,
chain_id,
ORDER BY user_id, first_ping
添加回答
举报