I need to write a query to show the number of tracks sold per artist per year for the top 10 best selling artists. Take the overall top 10 for all years.
This is what I have:
SELECT TOP 10 a.Name, YEAR(i.InvoiceDate), count(t.TrackId)
FROM Artist a
INNER JOIN Album al on a.ArtistId = al.ArtistId
INNER JOIN Track t on al.AlbumId = t.AlbumId
INNER JOIN InvoiceLine il on t.TrackId = il.TrackId
INNER JOIN invoice i on il.InvoiceId = i.InvoiceId
Group by YEAR(i.InvoiceDate), a.Name
ORDER by count(t.TrackId) DESC;
I don't only the top 10 of all the tracks sold by on group and not by each year the top 10.
Best Answer
You could use the
RANK()
windowing function to accomplish thisAdapted from this answer
Reproducable with this dataset
I like Lord Of The Rings, don't judge
Result
For each year, ordering by Counter desc, my dataset is not the best to get a clean result