Sql组加法

时间:2016-09-14 15:48:29

标签: mysql sql group-by

我有一个查询,我用“marketing_fee”提取“顾问”的所有记录。结果如下:

enter image description here

有没有办法按顾问(中间栏)对它们进行分组,但是加起来所有的marketing_fee?

我目前使用的代码只是收取最新费用。

select a.marketing_fee, v.Consultant, user.colorcode 
from valuations v 
join status_history sh on sh.ref = v.Ref 
join user on user.username = v.Consultant 
join answer a on a.title = v.Ref 
where sh.status = 'booked' and sh.date between '2016-09-07 23:59:00' and '2016-09-14 23:59:00'

3 个答案:

答案 0 :(得分:0)

这是你想要的吗?

<button md-button (click)="someFunc()" class="md-primary" layout-align="end center">Sort</button>

答案 1 :(得分:0)

尝试使用以下查询。

   select SUM(IFNULL(a.marketing_fee,0))Marketing_Fee,
               v.Consultant,
               User.Colorcode
    from valuations v 
        join status_history sh
              on sh.ref = v.Ref 
        join user on user.username = v.Consultant
        join answer a on a.title = v.Ref 
   where sh.status = 'booked' and
  sh.date between '2016-09-07 23:59:00' and '2016-09-14 23:59:00'
   group by v.Consultant,User.colorcode

答案 2 :(得分:-1)

我相信您可以按多列分组并在marketing_fee列上使用聚合函数。

select v.Consultant, SUM(a.marketing_fee), user.colorcode 
from valuations v 
join status_history sh on sh.ref = v.Ref join 
user on user.username = v.Consultant 
join answer a on a.title = v.Ref 
where sh.status = 'booked' and sh.date between '2016-09-07 23:59:00' and '2016-09-14 23:59:00'
group by v.consultant, a.marketing_fee, user.colorcode