两个group by 的sum相减
时间: 2023-09-28 19:07:05 浏览: 162
C#实现两个时间相减的方法
5星 · 资源好评率100%
对于两个GROUP BY语句的SUM相减,你可以使用子查询来实现。首先,在第一个GROUP BY语句中,你可以计算出deptno对应的sum(sal)值。然后,在第二个GROUP BY语句中,你可以计算出deptno和job对应的sum(sal)值。最后,将第二个GROUP BY的结果减去第一个GROUP BY的结果即可得到两个SUM的差值。
以下是具体的SQL查询语句:
SELECT t1.deptno, t2.job, t2.sum_sal - t1.sum_sal AS diff
FROM (
SELECT deptno, SUM(sal) AS sum_sal
FROM emp
GROUP BY deptno
) t1
JOIN (
SELECT deptno, job, SUM(sal) AS sum_sal
FROM emp
GROUP BY deptno, job
) t2
ON t1.deptno = t2.deptno
ORDER BY t1.deptno, t2.job;
这样,你就可以得到两个GROUP BY的SUM值的差值了。请注意,根据你提供的引用内容,我假设emp表是你的数据表,并且已经包含了相关的数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [oracle分组group by](https://blog.csdn.net/qq_40638598/article/details/90968185)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [mysql 怎么样获取两组数据相减的值](https://blog.csdn.net/weixin_35971787/article/details/113114582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文