OK folks, I have an urgent need. I am tasked with re-creating an accurate history of changes to multiple fields of an active table retroactively based on individual audit tables for each field. I have a SQL Server Agent job that does this currently on a daily basis starting 10/16/2012, but need to immediately add records to retrofit the history back to 1/1/2012.
To compound my issue, I need to reflect daily changes to the full hierarchy from the top down based on these audit records.
Here's what I have:
Active Table:
empId varchar(20),
first_name varchar(50),
last_name varchar(50),
cost_center varchar(20), -- need to re-create history
mgr_empId varchar(20), -- need to re-create history
start_dt date, -- starting date range of active record
end_dt date, -- end of active record - default value of 12/31/2999 = active record
hierarchy_empId varchar (200), --derived field from a nasty self-referencing function (12x)
hierarchy_name varchar(1000) -- derived from the same function as the field above.
Example:
Employee A1000, Tim Lee enters the org on 3/1/2012 with manager Bill A with emp Id B1000 into cost center 01552.
A1000 Tim Lee 01552 B1000 3/1/2012 12/31/2999 B1000>A1000 Bill A>Tim Lee B1000 Bill A 01552 NULL 1/1/2011 12/31/2999 B1000 Bill A
On 5/1/2012 the nightly job picks up a delta for Tim, and Tim's cost center changes to 31550. So his current record is updated with an end date of yesterday, and a new "current" record is entered, reflected below:
A1000 Tim Lee 01552 B1000 3/1/2012 4/30/2012 B1000>A1000 Bill A>Tim Lee B1000 Bill A 01552 NULL 1/1/2011 12/31/2999 B1000 Bill A A1000 Tim Lee 31550 B1000 5/1/2012 12/31/2999 B1000>A1000 Bill A>Tim Lee
On 6/15/2012, a new manager Ben C, emp Id C1000, was hired underneath Tim's manager:
A1000 Tim Lee 01552 B1000 3/1/2012 4/30/2012 B1000>A1000 Bill A>Tim Lee B1000 Bill A 01552 NULL 1/1/2011 12/31/2999 B1000 Bill A A1000 Tim Lee 31550 B1000 5/1/2012 6/14/2012 B1000>A1000 Bill A>Tim Lee C1000 Ben C 31550 A1000 6/15/2012 12/31/2999 A1000>C1000 Bill A>Ben C A1000 Tim Lee 31550 B1000 6/15/2012 12/31/2999 B1000>C1000>A1000 Bill A>Ben C>Tim Lee
These updates must trickle down the hierarchy. So, if something changes above Tim, it also must be reflected in his record.
On 7/15, Tim's original manager Bill A gets a new manager, Mark S, emp Id X3000.
A1000 Tim Lee 01552 B1000 3/1/2012 4/30/2012 B1000>A1000 Bill A>Tim Lee B1000 Bill A 01552 NULL 1/1/2011 7/14/2012 B1000 Bill A A1000 Tim Lee 31550 B1000 5/1/2012 6/14/2012 B1000>A1000 Bill A>Tim Lee C1000 Ben C 31550 A1000 6/15/2012 7/14/2012 A1000>C1000 Bill A>Ben C A1000 Tim Lee 31550 C1000 6/15/2012 7/14/2012 B1000>C1000>A1000 Bill A>Ben C>Tim Lee X3000 Mark S 01000 NULL 7/15/2012 12/31/2999 X3000 Mark S A1000 Tim Lee 31550 C1000 7/15/2012 12/31/2999 X3000>B1000>C1000>A1000 Mark S>Bill A>Ben C>Tim Lee B1000 Bill A 01552 X3000 7/15/2012 12/31/2999 X3000>B1000 Mark S>Bill A C1000 Ben C 31550 B1000 7/15/2012 12/31/2999 X3000>B1000>C1000 Mark S>Bill A>Ben C
I have these audit tables:
tblManagerAudit
empId varchar(20),
chgTimestamp datetime,
oldMgr varchar(20),
newMgr varchar(20)
tblCostCenterAudit
empId varchar(20),
chgTimnestamp datetime,
oldCostCenter varchar(20),
newCostCenter varchar(20)
I have struggled with how to combine the audit records for a very long time and appreciate any input.
I think this should get you going:
SELECT ma1.*, ma2.*
FROM ManagerAudit ma1
LEFT JOIN ManagerAudit ma2
ON ma1.empId = ma2.empId
and ma1.chgTimestamp < ma2.chgTimestamp
AND NOT EXISTS
(SELECT * FROM ManagerAudit ma3
WHERE ma3.empID = ma1.empID
AND ma3.chgTimestamp > ma1.chgTimestamp
AND ma3.chgTimestamp < ma2.chgTimestamp)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.