sql - Detecting changes within a dataset in mysql -
i have table has time-ordered values:
id date value
value
same hundreds of records @ strech, , i'd able determine when value
changes. in other words, know when d/dx (the derivative of data) not equal 0!
it seems there should common pattern can't find examples or come 1 myself. did find an example change detection done, can't use because database adapter pools connections , queries not issued on same connection. similarly, i'd rather not use database trigger.
here's example table:
id | date | value 1 | 2011-04-05 12:00 | 33 2 | 2011-04-06 12:00 | 39 3 | 2011-04-07 12:00 | 39 ... 72 | 2011-05-16 12:00 | 39 73 | 2011-05-17 12:00 | 37 74 | 2011-05-18 12:00 | 33 75 | 2011-05-19 12:00 | 33 ...
i'm looking query pull rows values change:
id | date | value 1 | 2011-04-05 12:00 | 33 2 | 2011-04-06 12:00 | 39 73 | 2011-05-17 12:00 | 37 74 | 2011-05-18 12:00 | 33
its not necessary first row included in result, since table identical values return 0 rows (i.e. there no changes).
select t.id, t.date, t.value, if( ( select td.value last_value tab td td.date < t.date order td.date desc limit 1 ) <> t.value, 'changed', 'unchanged' ) cstatus tab t order date
not efficient query, slow on large sets, trick. adding counter lets num_repeated column, updated on insert better solution.
Comments
Post a Comment