Conditionally Relate New Tables

时间:2019-01-18 18:40:12

标签: sql sql-server tsql database-design

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

1 个答案:

答案 0 :(得分:0)

在我看来,有多种设计解决方案可用,这取决于源数据的加载方式以及是否需要跟踪所有定期更改(在这种情况下,模型将需要日期时间支持)。

可能有多种选择,但是我将探索一种“星型模式”设计,该设计将需要构建您的宽泛且具有描述性的维表,以将PKey-FKey关系链接到记录所有交易的中央Fact表(在您的这种情况就是需要跟踪的各种“促销”价格)。

在您基于我的理解力的示例中,我将选择星型模式设计,其中包含项目,brandGroup和任何其他所需尺寸的维,以及用于跟踪库存的事实表和用于跟踪价格更新的事实表。通过将表格设计为一致的尺寸模型,我们可以在这个新仓库中进行所有类型的分析。

关于您的“ CigGroup”表,我专门为“ Items”创建一个具有最精细SKU /商品的表,然后可以使用属性或表中的新列将其构造为层次结构。