Hypothetical example:
I have an SQL table that contains a billion or so transactions:
| Cost | DateTime | | 1.00 | 2009-01-02 | | 2.00 | 2009-01-03 | | 2.00 | 2009-01-04 | | 3.00 | 2009-01-05 | | 1.00 | 2009-01-06 | ...
What I want is to pair down the data so that I only see the cost transitions:
| Cost | DateTime | | 1.00 | 2009-01-02 | | 2.00 | 2009-01-03 | | 3.00 | 2009-01-05 | | 1.00 | 2009-01-06 | ...
The simplest (and slowest) way to do this is to iterate over the entire table, tracking the changes. Is there a faster/better way to do this in SQL?
No. There is no faster way. You could write a query that does the same job but it will be much slower. You (as a developer) know that you need to compare a value only with its direct previous value, and there is no way to specify this with SQL. So you can do optimizations that SQL cannot. So I imagine the fastest is to write a program that streams the results from the disk, holding in RAM only the last valid value and the current one (filtering out every value that is equal to the last valid).