Showing only those rows where data changed.

Let’s say you have a table where the values of a particular column only changes slowly over time, perhaps a parameter of some sort. Now you want to write a query that shows only those rows of the table of when the value in that column changed.

Take the following table for example: It has 21 rows. The first column (DT) shows a date, the second column (VAL) shows a value that slowly changes over time:

Now write a query that only returns the rows for those dates where the value changes, i.e. 9/1 (the first row), 9/5, 9/9, 9/12, and 9/18.

To do this we could try to use the LAG function in the WHERE clause and only return those rows where VAL is different from VAL of the previous row. But since we cannot use a windowing function in a WHERE clause, we’ll have to put it into a factored subquery first:

[sourcecode language=”sql”]
WITH myqry AS
(SELECT dt
,val
,lag(val, 1, 0) over(ORDER BY dt) pre_val
FROM t1)
SELECT dt,val
FROM myqry
WHERE val != pre_val;
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *