DELETE FROM DT_COSTCENTRE
DELETE FROM LNK_CONTRACT_COSTCENTRE

INSERT INTO DT_COSTCENTRE
(Entity_Id, Entity_Start_Date, Entity_End_Date)
SELECT Entity_Id,
		Entity_Start_Date,
		(SELECT TOP 1 Entity_End_Date FROM DT_CONTRACT a WHERE a.Entity_Id = DTC.Entity_Id ORDER BY Entity_Start_Date DESC)
FROM DT_CONTRACT DTC
WHERE Ndx = (SELECT TOP 1 Ndx FROM DT_CONTRACT a WHERE a.Entity_Id = DTC.Entity_Id ORDER BY Entity_Start_Date ASC)

INSERT INTO LNK_CONTRACT_COSTCENTRE
SELECT Entity_Id,
		Entity_Id,
		Entity_Start_Date,
		(SELECT TOP 1 Entity_End_Date FROM DT_CONTRACT a WHERE a.Entity_Id = DTC.Entity_Id ORDER BY Entity_Start_Date DESC)
FROM DT_CONTRACT DTC
WHERE Ndx = (SELECT TOP 1 Ndx FROM DT_CONTRACT a WHERE a.Entity_Id = DTC.Entity_Id ORDER BY Entity_Start_Date ASC)
