SQL DWH create new foreign logic

51 Views Asked by At

for example, I have orderitems table where I have columns:

  • id int PK
  • cost decimal(10,4)
  • quantity int
  • product_name varchar(100)
  • sku varchar(100)

I want to move out distinct concatenated product_name and sku values to separate table. To have smth like this: table variants:

  • id identity(1,1) PK
  • variant_name varchar(200)

And in the final I want to have this table orderitems_fact:

  • id int PK
  • variant_name_id int FK reference to variants
  • cost decimal(10,4)
  • quantity int

I tried to populate variants table like this:

insert into variants
(variant_name )
select distinct 
product_name||'-'||sku as variant_name 
from orderitems

and then when populating orderitems_fact I did this:

insert into orderitems_fact 
(id, variant_name_id, cost, quantity )
select 
o.id,
v.id as variant_name_id 
o.cost,
o.quantity 
from orderitems o
join variants v on o.product_name||'-'||o.sku = v.variant_name 

so in the end I got expected table.

So the question, is this approach correct? Are there any others possibilities or technics ?

0

There are 0 best solutions below