Database clean up, discard unchanged values
Budget: €8 – €120 EUR
Need a DB command (or procedure) to save only the latest value when it has been changed. Whole purpose is to free up database space while still keeping all evidence about value flapping.
For example, if input:
clock | value
=============
1 | a
2 | a
3 | a
4 | b
5 | b
6 | c
7 | a
8 | a
9 | a
then after running script it will erase older values of kind, but still keep evidence if the same value comes back again.
Result must be:
clock | value
=============
3 | a
5 | b
6 | c
9 | a
Output should NOT blindly erase duplicates and keep latest one. This is wrong:
clock | value
=============
5 | b
6 | c
9 | a
Ideally there should be same SQL command/script/one-liner for MySQL and PostgreSQL. If an unified solution is not possible, then I need two snippets to solve task on each DB flavor.
Solution should work for MySQL 8, MariaDB 10.3+, PostgreSQL 12+
For example, if input:
clock | value
=============
1 | a
2 | a
3 | a
4 | b
5 | b
6 | c
7 | a
8 | a
9 | a
then after running script it will erase older values of kind, but still keep evidence if the same value comes back again.
Result must be:
clock | value
=============
3 | a
5 | b
6 | c
9 | a
Output should NOT blindly erase duplicates and keep latest one. This is wrong:
clock | value
=============
5 | b
6 | c
9 | a
Ideally there should be same SQL command/script/one-liner for MySQL and PostgreSQL. If an unified solution is not possible, then I need two snippets to solve task on each DB flavor.
Solution should work for MySQL 8, MariaDB 10.3+, PostgreSQL 12+