I have spent many hours thinking about a solution for my problem but I give up.
Let's imagine a table
user_id | occurred_at
-- OK match example
1 | 2020-01-01 08:00:00 <- First match of the set
1 | 2020-01-01 08:08:00 <- Second match (8 minutes away from the previous so OK)
1 | 2020-01-01 08:10:30 <- this already exceeds 10 minutes period so the set is excluded
-- Not matched example
1 | 2020-01-01 10:00:00 <- First match
1 | 2020-01-01 10:05:00 <- Second match (5 minutes away from the previous so OK)
1 | 2020-01-01 10:09:59 <- this fits into 10 minutes period so the set is matched (09:59 away altogether from 10:00:00)
-- Another OK (4 matched)
2 | 2020-01-01 14:23:00
2 | 2020-01-01 14:24:00
2 | 2020-01-01 14:26:00
2 | 2020-01-01 14:27:00
-- Not matched
3 | 2020-01-01 11:00:00
3 | 2020-01-01 11:01:00
3 | 2020-01-01 15:26:00
3 | 2020-01-01 18:00:00
-- User mismatch so set is not matched neither
3 | 2020-01-01 20:00:00
1 | 2020-01-01 20:01:00
2 | 2020-01-01 20:02:00
How one can query a table like this to find rows with at least N (=3 in this example) occurrences for the given user that occurred in a explicit minutes interval (=10 in this example)? I think a table example above explains it better.
user_id
has matching entries? (Or how many?) Or find alluser_id
that have at least one matching set of rows? Also, a validCREATE TABLE
statement would be nice for testing convenience. And always your version of Postgres, please. Oh, and don't use the misleading namedate
for atimestamp
column.users
table with one row peruser_id
? That would be helpful in the event you want to identify all users that pass the test.