Hi am trying to import data into my data warehouse and to simplify importing some data I want to make a new column in a table that will keep track of the most current value as I get new data.
My tables look like the following:
CREATE TABLE CustomerFact(
Id INT IDENTITY PRIMARY KEY
,CustomerId NVARCHAR(20) NOT NULL --Current Id of a Customer
);
--Track changes to CustomerId
CREATE TABLE CustomerHistoryFact(
Id INT IDENTITY PRIMARY KEY
,CurrentCustomerId NVARCHAR(20) --Most recent Customer Id
,CustomerId NVARCHAR(20) NOT NULL --Customer Id at time of record insert
,PrvsCustomerId NVARCHAR(20) NOT NULL --Customer Id at time of transaction
,PrvsCustomerIdEffectiveDate DATE NOT NULL --Date when PrvsCustomerId was effective
,PrvsCustomerIdObsoleteDate DATE NOT NULL --When the PrvsCustomerId was expired and replaced with a new Customer Id
);
The CustomerId, PrvsCustomerId, PrvsCustomerIdEffectiveDate, and PrvsCustomerIdObsoleteDate are all external. I want to assign a CurrentCustomerId whenever a new record is inserted.
This is determined by when a new CustomerHistoryFact is inserted, check the PrvsCustomerId with the CurrentCustomerId of all other records. If there is a match then the CustomerId of the new record becomes the CurrentCustomerId of all previous records or else the record is inserted and the CustomerId becomes the CurrentCustomerId.
Seems like a twisted database design. Why can’t you just keep on inserting
CustomerIdinCustomerHistoryFacttable, everytime theCustomerFactrecord is updated? You can write a trigger onCustomerFacttable for insert/update, and make an entry intoCustomerHistoryFacttable, if the latest CustomerId sorted byCustomerIdEffectiveDate descis different from the customerId in Inserted/Deleted table. That way, you have a history of CustomerId’s for starting from current CustomerId to back to in time.