I have a table in a SQL Server database which stores historical data on daily basis. The structure is shown below:
UploadDate TypeID Value1 Value2
-------------------------------------------
2012-01-08 1 NEG 1998-02-05
2012-01-08 2 NEG 1999-02-09
2012-01-08 3 STABLE 1997-02-06
2012-02-08 1 NEG 1998-02-05
2012-02-08 2 NEG 1999-02-09
2012-03-08 1 POS 2012-03-08
2012-03-08 2 STABLE 2012-01-08
As you can see above for the TypeID 1 & 2, Value1 and Value2 has changed on 2012-03-08
My requirement is such that I have to show only those rows which have changed from previous values.
In this case since TypeID 1 & 2 have changed than it should show the current and most nearest previous value. And for TypeID 3 since it has not changed, it will will only show the most current values. The result set would look something like below:
UploadDate TypeID Value1 Value2
-------------------------------------------
2012-01-08 3 STABLE 1997-02-06
2012-02-08 1 NEG 1998-02-05
2012-02-08 2 NEG 1999-02-09
2012-03-08 1 POS 2012-03-08
2012-03-08 2 STABLE 2012-01-08
Any idea how I can tackle this using SQL?
Uninspired version uses self-join on ordered set to check the value of chronologically previous row of the same typeid. If there is no previous row or values are different the row is output.
Here is Sql Fiddle with example.
UPDATE: another variant which does not require self-join but does require group by. Each timeline of same typeid, value1 and value2 are given unique group_number which is used later to extract max(uploaddate) for the group.
Another Sql Fiddle.