Untitled

 avatar
unknown
plain_text
a year ago
581 B
6
Indexable
WITH ranked_logs AS (
    SELECT 
        user_id, 
        time,
        LAG(time, 1) OVER (PARTITION BY user_id ORDER BY time) AS prev_time
    FROM logs
),
activity_periods AS (
    SELECT 
        user_id, 
        time, 
        CASE 
            WHEN prev_time IS NULL OR time - prev_time > 30 THEN 1
            ELSE 0
        END AS is_new_period
    FROM ranked_logs
),
user_activity_periods AS (
    SELECT
        user_id,
        SUM(is_new_period) AS cnt
    FROM activity_periods
    GROUP BY user_id
)
SELECT user_id, cnt
FROM user_activity_periods
ORDER BY user_id;
Editor is loading...
Leave a Comment