MySQL:Sum 2列在一起,条件基于2个其他字段按用户分组到新字段中

时间:2018-04-10 19:50:20

标签: mysql sql select sum conditional-statements

我知道,标题可能令人困惑,我写了它,甚至我不确定它真的意味着我想要的所以忍受我......

以下是我的表格中的几条记录(让我们称之为费用)的结果:

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

如您所见,还必须考虑日期范围。我已经阅读了很多关于此的帖子,看看如果我只希望每位员工有1条记录,并添加了#34;总计"专栏,但我似乎无法生成上述内容。

所有都在同一个数据库中。到目前为止,我的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

现在这已接近我想要的,最后一个问题是,如果它发生在日期范围内我没有得到任何特定的employee_name(所以结果应该是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

有没有办法预先定义&#34;总计&#34;零或某种条件适用于&#34;总计&#34;如果它是NULL,显示0?

3 个答案:

答案 0 :(得分:0)

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

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;

答案 1 :(得分:0)

您可以加入子查询以获得总计

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 

答案 2 :(得分:0)

最后,使用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

谢谢大家!!!