gpt4 book ai didi

mysql - 将两个 Select Sum 语句合并为一个

转载 作者:行者123 更新时间:2023-11-30 00:46:49 27 4
gpt4 key购买 nike

我的表中有两个语句,它们单独工作得很好,如下所示:

SELECT fee_earner, (SUM(fe_fees)) AS Total
FROM fees
GROUP BY fee_earner
order by Total desc;

SELECT supervisor, (SUM(sv_fees)) AS Total
FROM fees
GROUP BY supervisor
order by Total desc;

但有些情况下,fee_earner和supervisor字段的数据是同一个人,有没有办法将这两个语句合二为一,得到总体总数?

最佳答案

您可以使用union all来实现:

SELECT person, sum(fe_fees) as fe_fees, sum(sv_fees) as sv_fees,
(sum(fe_fees) + sum(sv_fees)) as total
FROM ((select fee_earner as person, fe_fees as fe_fees, 0 as sv_fees, 'earner' as which
from fees
) union all
(select supervisor as person, 0 as fe_fees, sv_fees as sv_fees, 'supervisor' as which
from fees
)
) t
GROUP BY person
order by Total desc;

关于mysql - 将两个 Select Sum 语句合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21297283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com