1

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?

13
  • 2
    How about adding VendorId to the OrderFact table?
    – Asdfg
    Commented Feb 8, 2013 at 19:04
  • @Asdfg the 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. Commented Feb 8, 2013 at 19:07
  • You may want to create a new table to map the vendor id and use that.
    – Asdfg
    Commented Feb 8, 2013 at 19:09
  • 1
    If I understand correctly, first you said that you want to track all orders from the same vendor but then you said that the vendor ID is not reliable. That means it isn't possible to achieve your aim because the data isn't good. Can you be more precise about what you want to do?
    – Pondlife
    Commented Feb 8, 2013 at 19:13
  • 2
    I don't have the full picture, but in that case I would add a 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.
    – Pondlife
    Commented Feb 8, 2013 at 19:27

1 Answer 1

0

I ended up creating a RelatedOrder table with a surrogate key as the primary key and the natural key as its columns. I then created a RelatedOrderMapping table for mapping a RelatedOrder by OrderId to the Fact table. Example (T-SQL for SQL Server 2008 R2):

--Create our RelatedOrder table which contains the Natural Key as its columns
CREATE TABLE RelatedOrder
(
     Id INT IDENTITY PRIMARY KEY
    ,VendorId INT NOT NULL
);
GO

--Create our mapping table for linking OrderFact to RelatedOrder
CREATE TABLE RelatedOrderMapping
(
     Id INT IDENTITY PRIMARY KEY
    ,RelatedOrderId INT NOT NULL REFERENCES RelatedOrder (Id)
    ,OrderFactId INT NOT NULL REFERENCES OrderFact (Id)
    ,OrderId INT NOT NULL
);
GO

--Insert one instance of each VendorId
INSERT INTO RelatedOrder (VendorId)
SELECT DISTINCT VendorId FROM OrderStaging;
GO

--Create a temp table to hold our output clause
CREATE TABLE #RelatedOrder
(
     Id INT IDENTITY PRIMARY KEY
    ,RelatedOrderId INT NOT NULL REFERENCES RelatedOrder (Id)
    ,OrderFactId INT NOT NULL REFERENCES OrderFact (Id)
    ,OrderId INT NOT NULL
);
GO

--Create our precomputed table to speed up the MERGE
CREATE TABLE #OrderStaging
(
     OrderId INT NOT NULL
    ,Amount INT NOT NULL
    ,Cost MONEY NOT NULL
    ,SaleDate DATE NOT NULL
    ,RelatedOrderId INT NOT NULL
);
GO

--Precompute our MERGE statement
INSERT INTO #OrderStaging(OrderId, Amount, Cost, SaleDate, RelatedOrderId)
SELECT OrderId, Amount, Cost, SaleDate, RelatedOrderId
FROM OrderStaging os
JOIN RelatedOrder ro
ON ro.VendorId = os.VendorId

--Insert our data into the fact table and output the result into our #RelatedOrder
MERGE OrderFact AS [Target]
USING #OrderStaging AS [Source]
ON 1 = 0
WHEN NOT MATCHED BY TARGET THEN
    INSERT(OrderId, Amount, Cost, SaleDate)
    VALUES([Source].OrderId, [Source].Amount, [Source].Cost, [Source].SaleDate)
    OUTPUT Inserted.Id, [Source].OrderId, [Source].RelatedOrderId
    INTO #RelatedOrder(OrderFactId, OrderId, RelatedOrderId)
;

--Insert our mappings
INSERT INTO RelatedOrderMapping(RelatedOrderId, OrderFactId, OrderId)
SELECT RelatedOrderId, OrderFactId, OrderId FROM #RelatedOrder;
GO

--Done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.