因为一人多岗导致的数据翻倍

人员和岗位的关系保存在维表dim_saler中,正常的关系是一个erp对应一个dept信息,但是由于某些原因,可能导致一个erp对应两条dept信息,这样在查询时会导致指标值double.

例如对于表dim_saler结构如下:

ID|Erp|Erp_name|Dept
---|:---|:---
1|wangerxiao|王二小|222
2|wangerxiao|王二小|333
3|liuchen|刘辰|222

事实表结构fact_table:

ID Erp qh_sl
1 wangerxiao 100
2 liuchen 200

对于sql语句

select b.erp_name, sum(a.qh_sl) from fact_table a, dim_saler b where a.erp = b.erp and b.erp='wangerxiao'

对于王二小同学,本来qh_sl为100,但是这种错误的纬度信息下会导致qh_sl double,为200

原因

对于MySQL,存在一对多(事实表中一条erp信息对应维表中多条维表信息)关系,导致生成的中间表为

id erp qh_sl id erp erp_name Dept
1 wangerxiao 100 1 wangerxiao 王二小 222
1 wangerxiao 100 2 wangerxiao 王二小 222

这样对qh_sl进行sum,会导致qh_sl失准

解决方案

找对唯一外键

posted on 2015-07-27 16:19  onesteng  阅读(147)  评论(0编辑  收藏  举报