I have a backend that log event to Redshift, for each event it generates a unique id. The id is a sequential number.
I have something like (events
table):
+-------------------------+------+
| created_at | id |
+-------------------------+------+
| 2017-06-30 09:20:47 UTC | 100 |
| 2017-06-30 09:18:31 UTC | 101 |
| 2017-06-30 09:16:19 UTC | 102 |
| 2017-06-30 09:12:08 UTC | 103 |
| 2017-06-30 09:11:59 UTC | 104 |
| 2017-06-30 09:11:15 UTC | 105 |
| 2017-06-30 07:03:41 UTC | 106 |
+-------------------------+------+
I have task than run every hour that move few records to an other table (deactivated_events
).
I would like to verify that for the last 3 hours I don't have any missing record using id
sequence.
First I thought about using generate_series but this is not present in Redshift. Other suggested to make a table with only the ids but that's still a pain to populate database with integer (here one example to generate 1 million
I'm wondering if the best is not use min max and count like this :
WITH merged_events AS
(SELECT *
FROM
(SELECT id, created_at
FROM events
UNION
SELECT id, created_at
FROM deactivated_events
)
WHERE created_at > GETDATE() - INTERVAL '3 hours'
ORDER BY id)
SELECT COUNT(*), (max(id) - min(id) + 1) AS diff
FROM merged_events;
PS: Bonus, how to found missing or duplicate records?