我有很多销售人员,他们属于部门。我想看看一个部门每天的销售额。
为简单起见,假设一名工人只属于一个部门。
例子:
部门:
| id | name |
| 1 | Men's Fashion |
| 2 | Women's Fashion |
工作人员:
| id | name |
| 1 | Timmy |
| 2 | Sally |
| 3 | Johnny |
销售量:
| id | worker_id | datetime | amount |
| 1 | 1 | 2013-1-1 08:00:00 | 1 |
| 2 | 1 | 2013-1-1 09:00:00 | 3 |
| 3 | 3 | 2013-1-2 08:00:00 | 8 |
部门_员工
| id | worker_id | department_id |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 2 |
我想得到
| department | amount |
| Men's Fashion | 4 |
| Women's Fashion | 8 |
要获得单个工人的总销售额,我可以
SELECT worker_id, SUM(amount) FROM sales
GROUP BY worker_id
我如何获取这些总和(每个工人的总销售额)并按部门汇总?