Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH
- total_sales_per_artist_per_country AS (
- SELECT
- C.Country,
- A.Name AS Artist,
- SUM(I.Total) AS total_sales
- FROM Customer C
- JOIN Invoice I
- ON C.CustomerId = I.CustomerId
- JOIN InvoiceLine IL
- ON I.InvoiceId = IL.InvoiceId
- JOIN Track T
- ON IL.TrackId = T.TrackId
- JOIN Album AL
- ON T.AlbumId = AL.AlbumId
- JOIN Artist A
- ON AL.ArtistId = A.ArtistId
- GROUP BY C.Country, A.Name
- ),
- max_sales_per_country AS (
- SELECT
- T.Country,
- MAX(T.total_sales) AS max_total_sales
- FROM total_sales_per_artist_per_country T
- GROUP BY T.Country
- )
- SELECT
- T.Country,
- T.Artist,
- T.total_sales
- FROM total_sales_per_artist_per_country T
- INNER JOIN max_sales_per_country M
- ON T.Country = M.Country
- AND T.total_sales = M.max_total_sales
Advertisement
Add Comment
Please, Sign In to add comment