I am trying to find a way to transform a natural key on some data that I am importing into some kind of surrogate key.
Example:
Say I have a table called OrderFact
which holds all of the information about my orders:
CREATE TABLE OrderFact
(
Id INT IDENTITY PRIMARY KEY
,OrderId INT NOT NULL
,Amount INT NOT NULL
,Cost MONEY NOT NULL
,SaleDate DATE NOT NULL
);
GO
INSERT INTO OrderFact (OrderId, Amount, Cost, SaleDate)
VALUES (1, 2, 12.00, '1/1/2012'), (3, 1, 6.00, '12/29/2011'), (4, 5, 1.00, '1/1/2012');
Now I get all of my order data from a POS system from some vendors, so my staging table looks like this:
CREATE TABLE OrderStaging
(
OrderId INT
,Vendor INT
,Amount INT
,Cost MONEY
,SaleDate DATE
);
GO
INSERT INTO OrderStaging (OrderId, Vendor, Amount, Cost, SaleDate)
VALUES (1, 1, 2, 12.00, '1/1/2012'), (3, 2, 1, 6.00, '12/29/2011'), (4, 1, 5, 1.00, '1/1/2012');
Now I don't care about who is placing the orders, but I want to keep track of which orders are place on the same day by the same vendor because they count as a Bulk Order
which I apply a special discount to.
Is there a way I can structure the database so I can keep track of which orders are bulk orders effectively transforming the Natural Key of Vendor
, SaleDate
into some kind of Surrogate key so I can lookup OrderFact.Id
against it?
VendorId
isn't really reliable at telling which orders belong to which vendor, so I thought it might be easier to just group the orders to prevent inconsistencies.BulkOrderID
column and populate it during or at the end of the staging process, based on whatever business rules you have. It isn't clear if this is a reporting system or not ('staging' and 'fact' suggest it is), but typically determining which orders are part of one bulk order is something that the order processing system does, not the reporting system. But every situation is different.