小编kjd*_*n84的帖子

sum() 按 distinct() id 列的列

我有以下查询:

select 
    sum(leads.source_cost) as cost, 
    coalesce(sum(assignments.recipient_revenue), 0) as revenue, 
    (coalesce(sum(assignments.recipient_revenue), 0) -
              sum(leads.source_cost)) as profit, 
    date_format(leads.updated_at, "%m/%d/%Y") as date 
from `leads` 
left join `assignments` on `assignments`.`lead_id` = `leads`.`id`
                       and `assignments`.`deleted_at` is null 
where leads.updated_at between "2017-08-24 04:00:00"
                           and "2017-08-26 03:59:59" 
group by `date`
Run Code Online (Sandbox Code Playgroud)

我需要在查询中sum(leads.source_cost)使用 distinct leads.id,因为可以多次分配潜在客户。

我该怎么做呢?我不能做 group by 因为我需要 group by 才能使用date别名。

mysql

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

mysql ×1