- Newest
- Most votes
- Most comments
A coworker of mine came up with the response that produces the results I need:
select user_id, ip_address, event_timestamp FROM (
SELECT *,
FLOOR(first_event_minutes_diff/30)
-FLOOR(LAG(first_event_minutes_diff) OVER (PARTITION BY user_id, ip_address ORDER BY event_timestamp)/30) new_diff,
CASE WHEN FLOOR(first_event_minutes_diff/30)
-FLOOR(LAG(first_event_minutes_diff) OVER (PARTITION BY user_id, ip_address ORDER BY event_timestamp)/30)
>= 1
OR FLOOR(first_event_minutes_diff/30)
-FLOOR(LAG(first_event_minutes_diff) OVER (PARTITION BY user_id, ip_address ORDER BY event_timestamp)/30)
IS NULL
THEN 1 ELSE 0 END AS is_session
FROM (
SELECT DISTINCT user_id,
ip_address,
event_timestamp::TIMESTAMP event_timestamp,
FIRST_VALUE(event_timestamp::TIMESTAMP)
OVER (PARTITION BY user_id, ip_address
ORDER BY event_timestamp::TIMESTAMP
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) first_event_timestamp,
DATEDIFF(SECOND, FIRST_VALUE(event_timestamp::TIMESTAMP)
OVER (PARTITION BY user_id, ip_address
ORDER BY event_timestamp::TIMESTAMP
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
event_timestamp::TIMESTAMP)/60.::DOUBLE PRECISION first_event_minutes_diff
FROM testschema.test_events)
ORDER BY 1,2,3) where is_session = 1
Here's a SQL query that should work for your use case in Amazon Redshift:
WITH ranked_events AS ( SELECT event_timestamp, user_id, ip_address, LAG(event_timestamp) OVER (PARTITION BY user_id, ip_address ORDER BY event_timestamp) AS prev_timestamp, ROW_NUMBER() OVER (PARTITION BY user_id, ip_address ORDER BY event_timestamp) AS rn FROM testschema.test_events ), filtered_events AS ( SELECT event_timestamp, user_id, ip_address, CASE WHEN rn = 1 OR EXTRACT(EPOCH FROM (event_timestamp - prev_timestamp)) / 60 >= 30 THEN 1 ELSE 0 END AS is_unique FROM ranked_events ) SELECT event_timestamp, user_id, ip_address, event_timestamp + INTERVAL '30 minutes' AS cool_off_until FROM filtered_events WHERE is_unique = 1 ORDER BY user_id, ip_address, event_timestamp;
This query works as follows:
-
The
ranked_eventsCTE uses window functions to:- Assign a row number to each event within each user_id and ip_address partition.
- Get the previous timestamp for each event using the LAG function.
-
The
filtered_eventsCTE then determines which events are unique based on the 30-minute cool-off period:- It marks an event as unique (is_unique = 1) if it's the first event for a user/IP combination or if it's been at least 30 minutes since the previous event.
- The EXTRACT function is used to calculate the time difference in minutes between events.
-
The final SELECT statement:
- Retrieves only the unique events (where is_unique = 1).
- Calculates the cool-off period end time by adding 30 minutes to the event timestamp.
- Orders the results as requested.
This query will return the unique records for each user_id and ip_address combination, considering the 30-minute cool-off period. The cool-off period is dynamically calculated for each event, ensuring that subsequent events are only considered unique if they occur after the cool-off period of the previous unique event.
Note that this solution uses standard SQL functions available in Amazon Redshift and doesn't require recursive CTEs, which can be less efficient for large datasets.
Sources
Date parts for date or timestamp functions - Amazon Redshift
EXTRACT function - Amazon Redshift
answered 2 years ago
Relevant content
asked a year ago
- AWS OFFICIALUpdated 6 months ago

This only returns the first record for each user/ip combo, not unique records that are at least 30 minutes apart, sorry AI.