mysql:sum 2列在一起,根据其他2个字段的条件由用户分组到一个新字段中

jei2mxaa  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(321)

我知道,标题可能会让人困惑,我写了它,甚至我不确定它真的意味着什么,我想让我忍受。。。
以下是我的表中几个记录的结果(我们称之为费用):

ID  employee_name      amountA amountB  valueA value B  billing_date
1   Luc                 0.15    5.00       0       0    2018-02-06
2   Luc                 0.00    2.85       0       0    2018-02-06
3   Luc                 0.00    3.15       0       1    2018-02-06
4   Anny               15.00    0.00       1       0    2018-02-06
5   Anny                0.00    0.35       0       0    2018-02-06
6   Anny               10.25    0.00       0       0    2018-02-06

我想要的是一个select语句,如果valuea和valueb都等于0,它将返回这个(上面)添加的新列,即sum(amounta)+sum(amountb)。因此:

ID  employee_name      amountA amountB  valueA value B  billing_date   total
1   Luc                 0.15    5.00       0       0    2018-02-06      8.00
2   Luc                 0.00    2.85       0       0    2018-02-06      8.00
3   Luc                 0.00    3.15       0       1    2018-02-06      8.00
4   Anny               15.00    0.00       1       0    2018-02-06     10.60 
5   Anny                0.00    0.35       0       0    2018-02-06     10.60
6   Anny               10.25    0.00       0       0    2018-02-06     10.60

如您所见,日期范围也必须考虑在内。我读过很多关于这个的帖子,如果我只希望每个员工有一条记录,加上“总计”一栏,那该怎么做,但我似乎无法生成上面的内容。
都在同一个数据库中。到目前为止,我的sql语句是这样的(基于下面的gordon linoff帮助):

select c.*,
   (select sum(c2.amountA) + sum(c2.amountB)
    from charges c2
    where c2.employee_name = c.employee_name and c2.valueA = 0 and c2.valueB = 0 and c2.billing_date <= '2018-02-06' and c2.billing_date >= '2018-02-06' 
   ) as total from charges c WHERE employee_name LIKE '%' and c.billing_date <= '2018-02-06' and c.billing_date >= '2018-02-06' 
order BY `c`.`employee_name` ASC

现在这已经接近我想要的了,最后一个问题是,如果在某个日期范围内,我没有得到某个特定员工的姓名(因此结果应该是0.00),那么我会得到null,如下所示:

ID  employee_name      amountA amountB  valueA value B  billing_date   total
1   Luc                 0.15    5.00       1       0    2018-02-06      NULL
2   Luc                 0.00    2.85       1       0    2018-02-06      NULL
3   Luc                 0.00    3.15       0       1    2018-02-06      NULL
4   Anny               15.00    0.00       1       0    2018-02-06      10.60 
5   Anny                0.00    0.35       0       0    2018-02-06      10.60
6   Anny               10.25    0.00       0       0    2018-02-06      10.60

有没有一种方法可以预先定义“total”为零,或者有某种条件应用于“total”,如果为空,则显示0?

5ssjco0h

5ssjco0h1#

一种方法使用相关子查询:

select c.*,
       (select sum(c2.amountA) + sum(c2.amountB)
        from charges c2
        where c2.employee_name = c.employee_name and c2.valueA = 0 and c2.valueB = 0
       ) as total
from charges c;

编辑:
如果你不想 NULL ,只需使用 coalesce() :

select c.*,
       coalesce((select sum(c2.amountA) + sum(c2.amountB)
                 from charges c2
                 where c2.employee_name = c.employee_name and c2.valueA = 0 and c2.valueB = 0
                ), 0
               ) as total
from charges c;
1sbrub3j

1sbrub3j2#

您可以加入total的子查询

select m.* , t.total 
from my_table m
left join (

    select employee_name, sum(amountA + amountB) total 
    from my_table
    where valueA = 0 and value B = 0 
    group by employee_name 
) t  on m.employee_name = t.example
ct2axkht

ct2axkht3#

最后,使用coalesce解决了我的最后一个问题:

select c.*,
coalesce((select sum(c2.amountA) + sum(c2.amountB) from charges c2
         where c2.employee_name = c.employee_name and c2.valueA = 0 and c2.valueB = 0 
         and c2.billing_date <= '2018-02-06' and c2.billing_date >= '2018-02-06'),'0.00') as total 
from charges c WHERE employee_name LIKE '%' and c.billing_date <= '2018-02-06' and c.billing_date >= '2018-02-06' 
order BY `c`.`employee_name` ASC

谢谢大家!!!

相关问题