I run a store that sells cigarettes, which have certain promotions given to us by the manufacturer that effect their pricing. These promotions are organized into groups of products (like all menthol or all reds) and are subject to frequent change, making them a bear to manage. My end goal here is to create a table(s) that will help me track these promotions and run an UPDATE query that will adjust their prices.
I have table inventory
like
itemnum|dept_id|cost |price
-----------------------------
123 | cig | 2.6 | 3.4
234 | 401 | 2.22| 23.4
345 | cig | 3.33| 3.45
456 | cig | 4.00| 4.56
567 | 901 | 4.5 | 5.67
678 | cig | 4.1 | 6.25
789 | cig | 5.2 | 6.25
My initial thought was creating a set of new tables like
CigGroup
Brand | Group_id | Itemnum
-------------------------------
Altria| a_men | 123
Altria| a_men | 345
Altria| a_black | 456
RJR | r_crush | 678
RJR | r_crush | 789
And
CigGroup_Promo
Group_id |promo_1|promo_2|promo_n...|net_promo|
--------------------------------------------
a_men | .5 | 1 | .1 | 1.6 (promo_1 + ...promo_n...)
a_red | .25 | 1 | NULL | 1.25
a_black | .25 | .5 | .1 | .85
r_crush | .25 | .1 | NULL | .35
r_filter | .35 | .5 | NULL | .85
I thought that maybe I could do something conditionally with foreign keys and set Cig_Group.Itemnum
to reference inventory.itemnum
only when inventory.itemnum = 'cig'
, though from SQL Server Conditional Foreign Key
I gathered that this might not be possible. (I've also looked into composite keys, but not sure how to apply this to my data)
So, here are my questions:
First, is it possible to populate my new table(s) (however that ends up being structured) with inventory.itemnum
only when inventory.dept_id = 'cig'
?
Second, can i set CigGroup_Promo.Net_Promo
as a function of promo_1
, promo_2
, promo_n...
, or is that yet another table that I would be creating?
Any suggestions on how to structure tables for these data and how to relate them would be greatly appreciated.
Side note: I could, instead of creating CigGroup
, create new values for inventory.dept_id
, which I would honestly prefer not to do, but might make things simpler.
Once all the tables are created and related, I'm hoping to be able to run something like:
UPDATE inventory i SET price =
CASE WHEN 1.07 * (i.cost - g.net_promo) >= .5 + (i.cost - g.net_promo)
THEN 1.07 * (i.cost - g.net_promo)
ELSE .5 + (i.cost - g.net_promo)
END
FROM inventory i JOIN GigGroup g ON i.itemnum = g.itemnum
JOIN CigGroup_Promo p ON g.group_id = p.group_id