由distinct()id列的列的sum()

时间:2017-08-25 00:01:41

标签: mysql sql

我有以下查询:

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";

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

我该怎么做?我不能分组,因为我需要分组来使用date别名。

0 个答案:

没有答案