A total for earlier events accidentally counted the next one
Our test log held four amounts. First came 1, then 2, then 8, then 4. The middle two entries had the same timestamp, but each also had a sequence number marking its place in the log. Just before the entry worth 2, only the 1 had been recorded.
We asked SQLite, a program for working with databases, to add up the earlier entries. One calculation returned 9. It had counted the 8 from the next entry as well as the 1. A second calculation used the recorded sequence to stop before the current entry. It returned 1.
In our SQLite 3.50.4 test, each event had a time, a sequence number showing its order in the log and a value. The numbers 9, 10 and 11 are sample timestamps. The first event was at time 9. At time 10 came sequence 1, worth 2, then sequence 2, worth 8. The fourth event came at time 11. How SQLite includes or excludes rows
Why the entry worth 8 was counted too soon
For each event, SQLite can add up the values in a chosen set of rows, called a window. Our first calculation ordered the rows by timestamp, event_time. That made the two entries at time 10 ties, even though their sequence numbers differed. The window instruction was RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW.
Read that from the point of view of the entry worth 2. The time range takes in everything earlier and all events at time 10. The last instruction removes the current event itself. The other event at time 10 stays, even though its sequence number puts it later. That leaves 1 plus 8, or 9.
| Timestamp | Sequence | Value | Timestamp only | With sequence |
|---|---|---|---|---|
9 | 0 | 1 | NULL | NULL |
10 | 1 | 2 | 9 | 1 |
10 | 2 | 8 | 3 | 3 |
11 | 3 | 4 | 11 | 11 |
The first event had nothing before it, so both totals were NULL, SQLite’s marker for no value to sum. For the event worth 2, the table shows 9 beside the intended 1. Both methods gave 3 for the next event and 11 for the last.
In a separate run with only the first two rows, before the later event worth 8 was present, both calculations gave 1 for sequence 1. Reversing the order in which we inserted the four records made no difference. Giving all four events distinct timestamps made the two methods agree on every row.
What counts as earlier in this log?
If timestamp order agrees with the log’s intended order but timestamps can tie, use a trustworthy sequence number that uniquely resolves each tie within its history. We sorted by time and then by the recorded sequence number. In a ROWS window, ending one row before the current event leaves out that event and everything later in the sequence. In the code below ORDER BY puts the events in that order and 1 PRECEDING stops one row early. That gives 1 for sequence 1. If the data contains separate histories, use PARTITION BY to keep each total within its own history. SQLite’s rules for window boundaries
sum(value) OVER (
ORDER BY event_time, event_sequence
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
)A different policy could require a strictly earlier timestamp. In that case neither of the events at time 10 belongs in the other’s running total, even when we know which arrived first. A timestamp-ordered RANGE window with EXCLUDE GROUP leaves out the entire current timestamp group. Choose the question first, then the instruction that answers it.
Repeat the four-event calculation
We used Python 3.13.17 and SQLite 3.50.4. Download the replay script, then run python -I -B replay.py from its folder to repeat the comparisons. The saved observations include the four invented events, the control runs and the SQLite source ID. Saved observations