0

imagine that I have 2 tables and I would like to calculate the best selling artists:

  • music (id, title, barcode)
  • 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

2
  • 1
    Erm, group by artist? Commented Jul 29, 2013 at 21:42
  • 1
    If you don't care about individual barcodes, why are you selecting and grouping them? Commented Jul 29, 2013 at 21:42

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.