Skip to content

Redshift-Return unique records by timestamp and userid using a dynamic cool-off period

0

I have a test table with event timestamps, userID and IP address. I am trying to get a list of unique events that occur for each userid and ip_address combination considering a 30 minute cool-off period (not events within a common 30 minute period like 4:00-4:30). For instance if the first event occurred at 4:17PM for UserA/IP1, it would not consider the next event as unique for UserA/IP1 until after 4:48PM, then the next unique record as valid 30 minutes after that event.

to recreate a sample dataset:

create table testschema.test_events (event_timestamp timestamp, user_id varchar, ip_address varchar);

insert into testschema.test_events (event_timestamp, user_id, ip_address)
values ('1/22/2025 16:05:34','192.168.0.1','userA')
    ,('1/22/2025 16:27:10','192.168.0.1','userA')
    ,('1/22/2025 16:37:13','192.168.0.1','userA')
    ,('1/22/2025 16:37:19','192.168.0.1','userA')
    ,('1/22/2025 16:38:09','192.168.0.1','userA')
    ,('1/22/2025 17:05:28','192.168.0.1','userA')
    ,('1/22/2025 17:09:35','192.168.0.1','userA')
    ,('1/22/2025 17:09:38','192.168.0.1','userA')
    ,('1/22/2025 17:12:23','192.168.0.1','userA')
    ,('1/22/2025 16:58:28','192.168.0.2','userB')
    ,('1/22/2025 16:58:34','192.168.0.2','userB')
    ,('1/22/2025 17:25:14','192.168.0.2','userB')
    ,('1/22/2025 17:37:12','192.168.0.2','userB')
    ,('1/22/2025 17:37:15','192.168.0.2','userB')
    ,('1/22/2025 17:37:33','192.168.0.2','userB')
    ,('1/22/2025 18:05:47','192.168.0.2','userB')
    ,('1/22/2025 18:05:57','192.168.0.2','userB')
    ,('1/22/2025 18:10:52','192.168.0.2','userB')
    ,('1/22/2025 18:27:46','192.168.0.2','userB')
    ,('1/22/2025 18:27:49','192.168.0.2','userB')
    ,('1/22/2025 18:50:15','192.168.0.2','userB')
    ,('1/22/2025 18:50:19','192.168.0.2','userB')
;

This would be my expected logical results. For each userid and ip_address combination, be able to identify the unique records, The cool-off period column is just for reference here to help identify when the next valid record should be considered for that userid/ip. I only need to return the unique records, not all records.

event_timestampip_addressuser_idcool-off untilaction
1/22/2025 16:05:34192.168.0.1userA1/22/2025 16:35:34include
1/22/2025 16:27:10192.168.0.1userAexclude
1/22/2025 16:37:13192.168.0.1userA1/22/2025 17:07:13include
1/22/2025 16:37:19192.168.0.1userAexclude
1/22/2025 16:38:09192.168.0.1userAexclude
1/22/2025 17:05:28192.168.0.1userAexclude
1/22/2025 17:09:35192.168.0.1userA1/22/2025 17:39:35include
1/22/2025 17:09:38192.168.0.1userAexclude
1/22/2025 17:12:23192.168.0.1userAexclude
1/22/2025 16:58:28192.168.0.2userB1/22/2025 17:28:28include
1/22/2025 16:58:34192.168.0.2userBexclude
1/22/2025 17:25:14192.168.0.2userBexclude
1/22/2025 17:37:12192.168.0.2userB1/22/2025 18:07:12include
1/22/2025 17:37:15192.168.0.2userBexclude
1/22/2025 17:37:33192.168.0.2userBexclude
1/22/2025 18:05:47192.168.0.2userBexclude
1/22/2025 18:05:57192.168.0.2userBexclude
1/22/2025 18:10:52192.168.0.2userB1/22/2025 18:40:52include
1/22/2025 18:27:46192.168.0.2userBexclude
1/22/2025 18:27:49192.168.0.2userBexclude
1/22/2025 18:50:15192.168.0.2userB1/22/2025 19:20:15include
1/22/2025 18:50:19192.168.0.2userBexclude

I've tried looking into recursive CTE's but I cannot get the syntax correct:

WITH RECURSIVE hit (user_id, ip_address, event_timestamp) AS (
   (
   SELECT user_id, ip_address, event_timestamp
   FROM   testschema.test_events
   ORDER  BY user_id, ip_address, event_timestamp
   )

   UNION ALL
   SELECT t.*
   FROM   hit h
   CROSS  JOIN  (
      SELECT t.user_id, t.ip_address, t.event_timestamp
      FROM   testschema.test_events t
      WHERE  (t.user_id, t.ip_address, t.event_timestamp)
           > (user_id, ip_address, event_timestamp + interval '30 minutes')
      ORDER  BY t.user_id, t.ip_address, t.event_timestamp
      LIMIT  1
      ) t
   )
SELECT * FROM hit;

but this does not return the expected results, just all records

asked 2 years ago143 views

2 Answers
1
Accepted Answer

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

answered 2 years ago

EXPERT

reviewed 2 years ago

-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:

  1. The ranked_events CTE 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.
  2. The filtered_events CTE 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.
  3. 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

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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.