I have two tables – cpm(cc, ad_id and cpm) and clicks(cc and ad_id).
When I run:
SELECT cpm.ad_id,
COUNT(clicks.id) * cpm.cpm AS clicks_income
FROM cpm
LEFT JOIN clicks on clicks.ad_id = cpm.ad_id and clicks.cc = cpm.cc
GROUP BY cpm.ad_id, cpm.cc
I get:
ad_id | clicks_income
---------------------
1 | 271.00
1 | 2.60
2 | 238.00
now the result I want is:
ad_id | clicks_income
---------------------
1 | 273.60
2 | 238.00
How can I group by only cpm.ad_id and having the SUM of all cc?
When I remove the cpm.cc I get:
ad_id | clicks_income
---------------------
1 | 273.00
2 | 238.00
Don’t use
COUNT(whatever) * acolumn, useSUM(AColumn)instead.Also a
LEFT JOINdoesn’t make sense here because a mismatch will just addNULLto the sum, which isn’t very helpful.