为了解决我的问题,我需要根据这些审核记录自上而下地反映每个完整层次结构的变化。
这就是我所拥有的:
活动表:
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
2012年5月1日,夜间工作为蒂姆增加了三角洲,蒂姆的成本中心变为31550.所以他的当前记录更新了昨天的结束日期,并输入了新的“当前”记录,反映如下:
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
2012年6月15日,一名新经理Ben C,emp Id C1000被聘用在Tim的经理之下:
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
这些更新必须逐步降低层次结构。所以,如果某些事情发生在蒂姆之上,那么它也必须反映在他的记录中。
在7月15日,蒂姆的原始经理比尔A得到了一位新经理,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
我有这些审核表:
tblManagerAudit
empId varchar(20),
chgTimestamp datetime,
oldMgr varchar(20),
newMgr varchar(20)
tblCostCenterAudit
empId varchar(20),
chgTimnestamp datetime,
oldCostCenter varchar(20),
newCostCenter varchar(20)
我一直在努力解决如何将审计记录整合很长时间并欣赏任何意见。
答案 0 :(得分:0)
我认为这应该让你前进:
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)