Reputation: 174
imagine that I have 2 tables and I would like to calculate the best selling artists:
artists (barcode, artist)
SELECT COUNT(m.barcode) AS sells, m.barcode, artist
FROM music m
LEFT JOIN artists a
ON m.barcode = a.barcode
GROUP BY m.barcode ORDER BY sells DESC
This query returns e.g.:
sells | barcode | artist
---------------------------------
1000 123 jdoe
500 223 kloe
100 321 jdoe
How can I calculate that jdoe complexively sold 1100 music?
Thanks in advance for any hint
Upvotes: 0
Views: 1008
Reputation: 1269883
You should change your group by
from barcode
to artist
(and remove barcode
from the select
statement):
SELECT COUNT(m.barcode) AS sells, artist
FROM music m LEFT JOIN
artists a
ON m.barcode = a.barcode
GROUP BY artist
ORDER BY sells DESC;
Upvotes: 1
Reputation: 2619
SELECT COUNT(m.barcode) AS popular, m.barcode, a.artist, SUM(a.sell)
FROM `music` AS m
LEFT JOIN `artists` AS a
ON (m.barcode = a.barcode)
WHERE a.artist = 'jdoe'
GROUP BY m.barcode
ORDER BY `popular` DESC
Upvotes: 0