mysql - SQL: selecting rows where column value changed from previous row -
let's have (mysql) database, sorted increasing timestamp:
timestamp system statusa statusb 2011-01-01 ok ok 2011-01-02 b ok ok 2011-01-03 fail fail 2011-01-04 b ok fail 2011-01-05 fail ok 2011-01-06 ok ok 2011-01-07 b fail fail how select rows statusa changed previous row system? statusb doesn't matter (i show in question illustrate there may many consecutive rows each system statusa doesn't change). in example above, query should return rows 2011-01-03 (statusa changed between 2011-01-01 , 2011-01-03 systema), 2011-01-06, 2011-01-07.
the query should execute table having tens of thousands of records.
thanks
select a.* tablex a.statusa <> ( select b.statusa tablex b a.system = b.system , a.timestamp > b.timestamp order b.timestamp desc limit 1 ) but can try (with index on (system,timestamp):
select system, timestamp, statusa, statusb ( select (@statuspre <> statusa , @systempre=system) statuschanged , system, timestamp, statusa, statusb , @statuspre := statusa , @systempre := system tablex , (select @statuspre:=null, @systempre:=null) d order system , timestamp ) statuschanged ;
Comments
Post a Comment