使用sql对tableau中的多对多关系中的数据进行规范化

时间:2015-09-25 21:30:39

标签: mysql sql left-join inner-join tableau

所以我正在尝试制作一个交互式画面仪表板。问题是我的两个表有重复的值,不能以一种很好的方式连接在一起(多对多关系创建重复)
这是我在3个表格中的原始数据:
员工信息 -

+-----------+--------+--------+
| Last_Name | Salary | Tenure |
+-----------+--------+--------+  
| Hoinski   | 40000  | 2      |  
| Smith     | 90000  | 4      |
| Jones     | 120000 | 10     |
+-----------+--------+--------+  

员工身份 -

+-----------+---------+----------+
| Last Name | Status  |   Date   |
+-----------+---------+----------+
| Jones     | On Time | 1/1/2015 |
| Jones     | On Time | 1/2/2015 |
| Jones     | On Time | 1/3/2015 |
| Jones     | On Time | 1/4/2015 |
| Jones     | Missing | 1/5/2015 |
| Hoinski   | On Time | 1/6/2015 |
| Hoinski   | Late    | 1/7/2015 |
| Hoinski   | Late    | 1/8/2015 |
| Hoinski   | Missing | 1/9/2015 |
+-----------+---------+----------+  

员工风险 -

+-----------+--------+-----------+
| Last Name |  Risk  |   Date    |
+-----------+--------+-----------+
| Jones     | High   | 2/10/2014 |
| Jones     | High   | 2/11/2014 |
| Jones     | Low    | 2/12/2014 |
| Jones     | Medium | 2/13/2014 |
| Jones     | Medium | 2/14/2014 |
| Jones     | Medium | 2/15/2014 |
| Jones     | Medium | 2/16/2014 |
| Smith     | Low    | 2/17/2014 |
| Smith     | Medium | 2/18/2014 |
| Smith     | Medium | 2/19/2014 |
| Smith     | Medium | 2/20/2014 |
| Hoinski   | High   | 2/21/2014 |
| Hoinski   | High   | 2/22/2014 |
| Hoinski   | Low    | 2/23/2014 |
+-----------+--------+-----------+  

因此,将信息表加入状态非常简单,将信息加入风险很容易。但我如何加入风险和地位呢?我需要加入他们吗?我想要建立的是3张图表,这些图表就像这张图片一样互相影响:
enter image description here
问题是当我点击一个饼图时,我无法更新其他饼图。我哪里错了?

1 个答案:

答案 0 :(得分:1)

我怀疑/希望您的风险和状态表每个员工的每个日期都有一行,并且您的信息表每个员工都有一行:

+-----------+--------+--------+
| Last_Name | Salary | Tenure |
+-----------+--------+--------+  
| Hoinski   | 40000  | 2      |  
| Smith     | 90000  | 4      |
| Jones     | 120000 | 10     |
+-----------+--------+--------+

+-----------+---------+----------+
| Last Name | Status  | Date     |
+-----------+---------+----------+
| Jones     | On Time | 1/1/2015 |
| Hoinski   | On Time | 1/1/2015 |
| Jones     | On Time | 1/2/2015 |
| Hoinski   | Late    | 1/2/2015 |
| Jones     | On Time | 1/3/2015 |
| Hoinski   | Late    | 1/3/2015 |
| Jones     | On Time | 1/4/2015 |
| Hoinski   | Missing | 1/4/2015 |
+-----------+---------+----------+

+-----------+--------+----------+
| Last Name | Risk   | Date     |
+-----------+--------+----------+
| Jones     | High   | 1/1/2015 |
| Smith     | Low    | 1/1/2015 |
| Hoinski   | High   | 1/1/2015 |
| Jones     | High   | 1/2/2015 |
| Smith     | Medium | 1/2/2015 |
| Hoinski   | High   | 1/2/2015 |
| Jones     | Low    | 1/3/2015 |
| Smith     | Medium | 1/3/2015 |
| Hoinski   | Low    | 1/3/2015 |
| Jones     | Medium | 1/4/2015 |
| Smith     | Medium | 1/4/2015 |
| Hoinski   | Low    | 1/4/2015 |
+-----------+--------+----------+

根据您提供的图片,您似乎有兴趣显示某位员工准时工作的天数(例如),以及他准时工作的那些天,那些天是多少天他的风险很高(例如)?为了显示风险和状态之间的关系,我们将不得不在我们的联接中包含日期。看起来您的状态表中可能没有每个员工,所以我们要进行左连接。

SELECT *
FROM EMPLOYEE_RISK
LEFT JOIN EMPLOYEE_STATUS 
ON EMPLOYEE_STATUS.Last_Name = EMPLOYEE_RISK.Last_Name
  AND EMPLOYEE_STATUS.Date = EMPLOYEE_RISK.Date
LEFT_JOIN EMPLOYEE_INFO
  ON EMPLOYEE_RISK.Last_Name = EMPLOYEE_INFO.Last_Name;

我们最终会得到这样的结论:

+--------+-----------+--------+---------+--------+--------+
| Date   | Last Name | Risk   | Status  | Salary | Tenure |
+--------+-----------+--------+---------+--------+--------+
| 1/1/15 | Jones     | High   | On Time | 120000 | 10     |
| 1/1/15 | Smith     | Low    | NULL    | 90000  | 4      |
| 1/1/15 | Hoinski   | High   | On Time | 40000  | 2      |
| 1/2/15 | Jones     | High   | On Time | 120000 | 10     |
| 1/2/15 | Smith     | Medium | NULL    | 90000  | 4      |
| 1/2/15 | Hoinski   | High   | Late    | 40000  | 2      |
| 1/3/15 | Jones     | Low    | On Time | 120000 | 10     |
| 1/3/15 | Smith     | Medium | NULL    | 90000  | 4      |
| 1/3/15 | Hoinski   | Low    | Late    | 40000  | 2      |
| 1/4/15 | Jones     | Medium | On Time | 120000 | 10     |
| 1/4/15 | Smith     | Medium | NULL    | 90000  | 4      |
| 1/4/15 | Hoinski   | Low    | Missing | 40000  | 2      |
+--------+-----------+--------+---------+--------+--------+

现在我们每个员工每天都有一份记录,每个维度都有。现在您已经有了一个原始的,非原始数据表,您应该能够在每个工作表上点击用作过滤器,以使这些过滤器正常工作。