我在从这些表中提取数据时遇到问题。我有一个统计系统,每个月中的每一天都会为员工输入数字-举例来说,这里只有两个月,但这将是一整年。我尝试加入,但结果却翻了一番,而且有些员工可以使用更多帐户,而其他员工只能使用一个...
员工:
+----+-------------+--+
| ID | Name | |
+----+-------------+--+
| 1 | John Doe | |
| 2 | Helen Price | |
| 3 | John Price | |
+----+-------------+--+
帐户
+----+---------+--+
| ID | Name | |
+----+---------+--+
| 1 | fisher | |
| 2 | fisher1 | |
+----+---------+--+
一月
+----+-------------+---------+----------+----------+----------+
| ID | Employee | Account | firstday | seconday | thirdday |
+----+-------------+---------+----------+----------+----------+
| 1 | John Doe | fisher | 2 | 4 | 6 |
| 2 | Helen Price | fisher | 1 | 1 | 1 |
| 3 | John Price | fisher1 | 1 | 2 | 1 |
| 4 | John Price | fisher | 1 | 1 | 1 |
+----+-------------+---------+----------+----------+----------+
2月
+----+-------------+---------+-----------+----------+-----------+
| ID | Employee | Account | fourthday | eightday | secondday |
+----+-------------+---------+-----------+----------+-----------+
| 1 | John Doe | fisher | 1 | 4 | 6 |
| 2 | Helen Price | fisher | 1 | 1 | 2 |
| 3 | John Price | fisher1 | 1 | 2 | 1 |
| 4 | John Price | fisher | 1 | 1 | 1 |
+----+-------------+---------+-----------+----------+-----------+
我的问题是如何编写具有以下结果的查询:
+----+-------------+---------+-----------------+------------------+--------+--+
| ID | Employee | Account | January(totals) | February(totals) | Totals | |
+----+-------------+---------+-----------------+------------------+--------+--+
| 1 | John Doe | fisher | 12 | 11 | 23 | |
| 2 | Helen Price | fisher | 3 | 4 | 7 | |
| 3 | John Price | fisher | 3 | 3 | 6 | |
+----+-------------+---------+-----------------+------------------+--------+--+
您想要的是来自不同JOIN
表的汇总结果的数据透视表。有两种方法,但一种方法是将LEFT JOIN
与子查询汇总使用,以汇总每个员工和每个帐户的月度结果。
在您的样本数据中,[Month]表之外没有Employee
与Account
的关联。因此,需要通过使用另一个子查询来创建关联来创建关联。除非创建用于创建关联的多对多表,否则这将导致每个员工和每个帐户都有关联。
最后,由于您的示例数据仅显示单个帐户(fisher
,因此您需要以某种方式对其进行过滤,这在您的问题中并未明确指定,因此我将其添加到WHERE
标准中。
SELECT
e.ID,
e.Name AS Employee,
e.Account,
COALESCE(janurary_totals.total, 0) AS `January(totals)`,
COALESCE(february_totals.total, 0) AS `February(totals)`,
COALESCE(janurary_totals.total, 0) + COALESCE(february_totals.total, 0) AS Totals
FROM (
SELECT e1.ID, e1.Name, a1.Name AS Account
FROM employees AS e1
LEFT JOIN Account AS a1
ON a1.ID IS NOT NULL
) AS e
LEFT JOIN (
SELECT
Employee,
Account,
SUM(firstday) + SUM(seconday) + SUM(thirdday) AS total
FROM January
GROUP BY Employee, Account
) AS janurary_totals
ON janurary_totals.Employee = e.Name
AND janurary_totals.Account = e.Account
LEFT JOIN (
SELECT
Employee,
Account,
SUM(fourthday) + SUM(eightday) + SUM(secondday) AS total
FROM February
GROUP BY Employee, Account
) AS february_totals
ON february_totals.Employee = e.Name
AND february_totals.Account = e.Account
WHERE e.Account = 'fisher'
结果
| ID | Employee | Account | January(totals) | February(totals) | Totals |
| --- | ----------- | ------- | --------------- | ---------------- | ------ |
| 1 | John Doe | fisher | 12 | 11 | 23 |
| 2 | Helen Price | fisher | 3 | 4 | 7 |
| 3 | John Price | fisher | 3 | 3 | 6 |
您可以通过将各个月份的表重构为带有DATETIME
列的单个employee_entry表,从而极大地简化事情,您可以使用该表查询创建数据透视表。