I’m trying to write the logic for a Query, that will allow me to classify users activities:
• The problem is a table that contain all users activities in slots of ~5 min (not all are exactly 5 min, some 3 min, others 4 min) and record the amount of time that each user spent in a certain status.
• The user usually jump between status across the day.
The issue: If a user spent more than 3 hours (180 min) continuously without a change in their status it must be reported as: “Unclassified”
Current view of the table that I’m working:
user_id record_date user_status
user1 9/3/2017 14:25 status_1
user1 9/3/2017 14:30 status_3
user1 9/3/2017 14:35 status_3
user1 9/3/2017 14:40 status_2
user1 9/3/2017 14:45 status_2
user1 9/3/2017 14:50 status_2
user1 9/3/2017 14:55 status_2
user1 9/3/2017 15:00 status_2
user1 9/3/2017 15:05 status_2
user1 9/3/2017 15:10 status_2
user1 9/3/2017 15:15 status_2
user1 9/3/2017 15:20 status_2
user1 9/3/2017 15:25 status_2
user1 9/3/2017 15:30 status_2
user1 9/3/2017 15:30 status_2
user1 9/3/2017 15:35 status_2
user1 9/3/2017 15:40 status_2
user1 9/3/2017 15:43 status_3
user1 9/3/2017 15:45 status_3
user1 9/3/2017 15:50 status_2
user1 9/3/2017 15:50 status_2
user1 9/3/2017 15:55 status_2
user1 9/3/2017 16:00 status_2
user1 9/3/2017 16:00 status_2
user1 9/3/2017 16:04 status_2
I started testing the following logic, but once I discovered that each slot is not exactly 5 min I was not able to continue.
SELECT user_id ,record_date
,CASE
WHEN SUM(status_1) OVER (
PARTITION BY user_id ORDER BY record_date ASC ROWS BETWEEN 35 PRECEDING
AND current row
) >= 180
THEN 1
ELSE 0
END AS unclassified_flag
--2
,CASE
WHEN SUM(status_2) OVER (
PARTITION BY user_id ORDER BY record_date ASC ROWS BETWEEN 35 PRECEDING
AND current row
) >= 180
THEN 1
ELSE 0
END AS unclassified_flag
FROM table
Any ideas of alternatives logic are truly appreciated