好的,伙计们,我有急事。我的任务是根据每个字段的各个审核表,重新创建活动表中多个字段的更改的准确历史记录。我有一个SQL Server代理作业,从2012年10月16日起每天都会执行此操作,但需要立即添加记录以将历史记录重新设置为2012年1月1日。
为了解决我的问题,我需要根据这些审计记录,从上到下反映对整个层次结构的每日更改。
以下是我所拥有的:
活动表:

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.

例子:
员工A1000,Tim Lee于2012年3月1日与经理Bill A(EMP ID B1000)一起进入组织,进入成本中心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)

很长一段时间以来,我一直在纠结如何将审计记录合并起来,并欣赏任何输入。

最佳答案

我想这会让你走的:

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)

10-07 16:27