I have a module which handles some business rules in my application, there are a few tables where these rules are stored.
Orignal Business Rule table :
br_tbl_1
br_id | col_1 | col_2
------+-------+--------
1 | a | myk
2 | b | abc
Related Tables: br_tbl_2
id | br_id | col_1
---+-------+--------
1 | 1 | something
2 | 1 | something_else
3 | 2 | Another thing
and so on...
Now to track the changes made to the business rules, I have an audit table for each of the above tables, like so..
Business Rule Audit Table: br_tbl_1_audit
id | br_id | col_1 | col_2 | audit_dtme | operation
---+-------+--------+-------+---------------------+-----------------
1 | 1 | a | xyz | 01-01-2001 12:30:10 | INSERT
2 | 1 | a | myk | 02-01-2001 01:00:00 | UPDATE
3 | 2 | b | abc | 02-01-2001 01:10:30 | INSERT
by looking at the data from br_tbl_1_audit table we can see that the value for col_2 for br_id = 1 has changed from "xyz" to "myk"
Similarly we have an audit table for the other business rules tables.
Related Table's Audit Table: br_tbl_2_audit
id | br_id | col_1 | audit_dtme | operation
---+-------+------------------+----------------------+--------------
1 | 1 | something | 01-01-2001 12:30:10 | INSERT
2 | 1 | something_else | 01-01-2001 12:30:10 | INSERT
3 | 2 | Another thing | 02-01-2001 01:10:30 | INSERT
I need a Query which takes in a br_id and an audit_date_time and rolls back all the data for that br_id in all tables to that audit_dtme
I can do this with a Script, however I am not very good with SQL Queries, I appriciate the help.
FYI : I am using Postgres, but any SQL should be enough t push me in the right direction.