Just so that I understand the problem.
You have, if you order the row set based on the timestamp, sometimes duplicate values occuring next to each other, like the above pair of 1's in the 2nd and 3rt item? and then you have double 0's in the 4th and 5th, is that it?
And you want the last of the corresponding pair (or sequence if there are more than 2)?
Why do you need to remove them? I'm asking because unless they occupy a significant share of the size of this table, it might be easier to filter them out like you do sequentially when you need to process or display them.
A solution, though not a very good one, would be to retrieve the minimum timestamp above the timestamp of the current row you're examining, and then retrieve the value from that, and if it's the same, don't return the current row.
Here's the SQL to get everything:
SELECT timestamp, value
FROM yourtable
And here's how to join in to get the minimum timestamp above the current one:
SELECT T1.timestamp, MIN(T2.timestamp) AS next_timestamp, T1.value
FROM yourtable T1, yourtable T2
WHERE T2.timestamp > T1.timestamp
GROUP BY T1.timestamp, T1.value
(I fear the above query will be horribly slow)
And then to retrieve the value corresponding to that minimum timestamp
SELECT T3.timestamp, T3.value
FROM (
SELECT T1.timestamp, MIN(T2.timestamp) AS next_timestamp, T1.value
FROM yourtable T1, yourtable T2
WHERE T2.timestamp > T1.timestamp
GROUP BY T1.timestamp, T1.value
) T3, yourtable AS T4
WHERE T3.next_timestamp = T4.timestamp
AND T3.value <> T4.value
Unfortunately this doesn't produce the last value, as it needs a following value to compare against. A simple dummy sentinel-value (you can union that in if you need to) will handle that.
Here's the sqlite database dump I tested the above query against:
BEGIN TRANSACTION;
CREATE TABLE yourtable (timestamp datetime, value int);
INSERT INTO "yourtable" VALUES('2008-09-22 16:28:14.133',0);
INSERT INTO "yourtable" VALUES('2008-09-22 16:28:35.233',1);
INSERT INTO "yourtable" VALUES('2008-09-22 16:29:16.353',1);
INSERT INTO "yourtable" VALUES('2008-09-22 16:31:37.273',0);
INSERT INTO "yourtable" VALUES('2008-09-22 16:35:43.134',0);
INSERT INTO "yourtable" VALUES('2008-09-22 16:36:39.633',1);
INSERT INTO "yourtable" VALUES('2008-09-22 16:41:40.733',0);
INSERT INTO "yourtable" VALUES('2099-12-31 23:59:59.999',2);
COMMIT;
And here is the (formatted) output:
timestamp value
2008-09-22 16:28:14.133 0
2008-09-22 16:29:16.353 1
2008-09-22 16:35:43.134 0
2008-09-22 16:36:39.633 1
2008-09-22 16:41:40.733 0