有没有办法从多个UNION查询中求出总和?我创建了多个表的UNION,它返回一个包含多行的表,每行都显示一份薪水。
+-------------+--------+
| employee_ID | Salary |
+-----+-------+--------+
| 0001 | 630 |
| 0002 | 480 |
| 0003 | 600 |
| 0004 | 600 |
| 0005 | 600 |
+-----+----------------+有没有一种方法可以在不创建视图的情况下对所有这些行(薪水)进行求和?它看起来像这样:
+-------------+--------+
| employee_ID | Salary |
+-----+-------+--------+
| 0001 | 630 |
| 0002 | 480 |
| 0003 | 600 |
| 0004 | 600 |
| 0005 | 600 |
| | 2910 |
+-----+----------------+发布于 2020-03-27 00:22:00
您可以使用rollup
select employee_id, sum(salary) as salary
from (<your union query here>) x
group by employee_id with rollup;发布于 2020-03-27 00:22:19
嗯,理想情况下,我会在我的演示工具中处理这样的事情。但基于您的简单示例:
select
employee_id,
sum(salary) as salary
from
<your table>
group by employee_id
UNION ALL
select
'' , -- or 'TOTAL' or whatever
sum(salary)
from
<your table>https://stackoverflow.com/questions/60871592
复制相似问题