首页 > 解决方案 > 使用 pivot 和 unpivot 分解头数的非规范化

问题描述

我有角色和 master_headcount 表。
在角色表中,我有 16 行(不确定)。
在 master_headcount 表中,我根据工作类型、月份和年份进行了细分

role table:

Id  name
1   Role1
2   Role2

master_headcount table:

Id  headcountvalue  workingtype month   year
1   3               fulltime    1       2019
2   5               parttime    1       2019
3   10              fulltime    2       2019
4   15              parttime    2       2019

master_headcount-role table:

master_headcountId  roleId  rolebased_headcountvalue
"1,2"               1       2
"1,2"               2       6
"3,4"               1       11
"3,4"               2       14

在应用程序中,我有这个 ui 到“CRUD”相关记录:

在此处输入图像描述

我需要为角色分解 master_headcount 表行。

我可以想象一个解决方案将行添加到主表,但是我想为“非规范化”表创建单独的表。

我也可以猜想我需要对我不擅长的操作进行旋转/取消旋转操作。至少对于旋转操作,我需要为我的需求建模。

我的问题是如何master_headcountId对没有“1,2”或“3,4”值的表格进行建模和设计

标签: sqldatabase-design

解决方案


我不是 100% 确定我理解了这个问题,但如果我理解了,那么你只需要在表中包含两倍的行master_headcount-role数。

这个:

master_headcountId  roleId  rolebased_headcountvalue
"1,2"               1       2

会变成这样:

master_headcountId  roleId  rolebased_headcountvalue
1                   1       2
2                   1       2

对于所有其他喜欢的行,依此类推。


推荐阅读