I have a table that's ordered by timestamp, and I only want to keep steps of a consecutive order (marked with * below).
In imperative programming, it would be:
prev_step = 0
output = []
for step in table.steps: # already sorted by timestamp
if step == prev_step + 1:
output.append(step) # desired row
prev_step = step
Original table that I have (desired rows decorated with *, not actually data):
| timestamp | step |
| --------- | ---- |
| 100000001 | 5 |
| 100000002 | 1 |*
| 100000003 | 1 | ^
| 100000004 | 2 |*
| 100000005 | 2 | ^
| 100000006 | 4 |
| 100000007 | 5 |
| 100000008 | 3 |*
| 100000009 | 4 |*
| 100000010 | 2 |
| 100000011 | 5 |*
| 100000012 | 7 |
What I want:
| timestamp | step |
| --------- | ---- |
| 100000002 | 1 |*
| 100000004 | 2 |*
| 100000008 | 3 |*
| 100000009 | 4 |*
| 100000011 | 5 |*
I've only managed to come up with a WHERE step - LAG(step) OVER (ORDER BY timestamp) <> 0, but that only removes adjacent duplicates (marked in ^ above). It certainly helps, but isn't quite there yet.
Thanks in advance!
