我有这个表:
[SDate - DateTime] [Value] - Represents a boolean 2010-11-16 10:10:00 1 2010-11-16 10:11:00 0 2010-11-16 10:12:00 1 2010-11-16 10:13:00 1
例如:
2010-11-16 10:10:00 0 2010-11-16 10:11:00 1 2010-11-16 10:12:00 0 2010-11-16 10:13:00 0 2010-11-16 10:14:00 1 2010-11-16 10:15:00 0 2010-11-16 10:16:00 1 ... 5 changes
2010-11-16 10:10:00 0 2010-11-16 10:11:00 0 2010-11-16 10:12:00 0 2010-11-16 10:13:00 1 2010-11-16 10:14:00 1 2010-11-16 10:15:00 1 2010-11-16 10:16:00 1 ... 1 change
解决方法
你可以用lag():
select count(*) from (select t.*,lag(value) order by (sdate) as prev_value from table t ) t where prev_value <> value ;