I have part of a table like this:
timestamp | Source
----------------------------+----------
2017-07-28 14:20:28.757464 | Stream
2017-07-28 14:20:28.775248 | Poll
2017-07-28 14:20:29.777678 | Poll
2017-07-28 14:21:28.582532 | Stream
I want to achieve this:
timestamp | Source
----------------------------+----------
2017-07-28 14:20:28.757464 | Stream
2017-07-28 14:20:29.777678 | Poll
2017-07-28 14:21:28.582532 | Stream
Where the 2nd row in the original table had been removed, because it's within 50ms of a timestamp before or after it. Important is only removes rows when Source = 'Poll'.
Not sure how this can be achieved with a WHERE clause maybe?
Thanks in advance for any help.
Whatever we do, we can limit that to Pools, then union those rows with Streams.
To get pools, there are different options:
Correlated subquery
For each row we run extra query to get the previous row with the same source, then select only those rows where there is no previous timestamp (first row) or where previous timestamp is more than 50ms older.
https://www.db-fiddle.com/f/iVgSkvTVpqjNZ5F5RZVSd2/2
Join two sliding tables
Instead running subquery for each row, we can just create a copy of our table and slide it so each Pool row joins with the previous row of the same source type.
https://www.db-fiddle.com/f/gXmSxbqkrxpvksE8Q4ogEU/2
Sliding window
Modern SQL can do something similar, with partitioning by source, then using sliding window to join with the previous row.
https://www.db-fiddle.com/f/8KfTyqRBU62SFSoiZfpu6Q/1
I believe this is the most optimal.