【问题标题】:MySQL: Sum 2 column together, condition based on 2 other fields group by user into a new fieldMySQL:将 2 列加在一起,基于 2 个其他字段的条件由用户分组为一个新字段
【发布时间】:2018-09-20 13:23:02
【问题描述】:

我知道,标题可能令人困惑,我写了它,甚至我不确定它是否真的意味着我想要的所以请耐心等待......

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

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 条记录并添加“总计”列,但我似乎无法生成上述内容,请查看如何操作。

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

有没有办法用零或某种条件预定义“总计”以应用于“总计”,如果它为 NULL,则显示 0?

【问题讨论】:

    标签: mysql sql select sum conditional-statements


    【解决方案1】:

    你可以加入总的子查询

    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 
    

    【讨论】:

    • 可悲的是,这给了我同一用户的重复结果记录,当我使用 LIKE '%' 作为员工姓名时,我得到的总数相同。感谢您尝试...
    • 您的评论似乎与您提供的示例无关.. 如果您使用其他示例,则不可能为您建议有效的解决方案
    【解决方案2】:

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

    谢谢大家!!!

    【讨论】:

      【解决方案3】:

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

      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;
      

      【讨论】:

      • 这是迄今为止最接近的。但是随着我添加了一些规范,我遇到了一个不同的问题......请参阅新帖子。
      猜你喜欢
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多