Guest User

Codex generated code

a guest
Sep 22nd, 2022
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.87 KB | None | 0 0
  1. WITH
  2. total_sales_per_artist_per_country AS (
  3.     SELECT
  4.         C.Country,
  5.         A.Name AS Artist,
  6.         SUM(I.Total) AS total_sales
  7.     FROM Customer C
  8.     JOIN Invoice I
  9.         ON C.CustomerId = I.CustomerId
  10.     JOIN InvoiceLine IL
  11.         ON I.InvoiceId = IL.InvoiceId
  12.     JOIN Track T
  13.         ON IL.TrackId = T.TrackId
  14.     JOIN Album AL
  15.         ON T.AlbumId = AL.AlbumId
  16.     JOIN Artist A
  17.         ON AL.ArtistId = A.ArtistId
  18.     GROUP BY C.Country, A.Name
  19. ),
  20. max_sales_per_country AS (
  21.     SELECT
  22.         T.Country,
  23.         MAX(T.total_sales) AS max_total_sales
  24.     FROM total_sales_per_artist_per_country T
  25.     GROUP BY T.Country
  26. )
  27.  
  28. SELECT
  29.     T.Country,
  30.     T.Artist,
  31.     T.total_sales
  32. FROM total_sales_per_artist_per_country T
  33. INNER JOIN max_sales_per_country M
  34.     ON T.Country = M.Country
  35.     AND T.total_sales = M.max_total_sales
Advertisement
Add Comment
Please, Sign In to add comment