If you have a table which holds history (updates, deletes) from another table, how would you pull an associated update row if it comes after a delete operation?
Table has the following columns not related to the data
ID int, — Identity
OP varchar — the delete or update operation coded as ‘D’ or ‘U’
Removed_Date — the date the row was removed from the ‘Live’ table
I’ve tried something like this
Select *
from historytable as table1
join historytable as table2 on table1.Removed_Date like table2.Removed_Date
which results in multiple rows (ten or more) of duplicates for one row in the table.
Here is what I ended up doing in Perl:
while (<>) {
@cols = split(/,/);
$removeby = $cols[3];
if ($cols[1] eq ‘D’ and $removeby!~/ottr|hksys/) {
if ($hold) {
@oldCols = split(/,/,$hold);
$oldRemoveBy = $oldCols[3];
if ($removeby eq $oldRemoveBy) {
print $hold;
print;
}
$hold = ”;
}
$hold = ”;
} else {
$hold = $_;
}
}
It’s not a fool-proof solution, but it seems to work.