bozu00
6/17/2018 - 4:29 PM

test.sql

test.sql

SELECT invoices.billingCountry, genres.Name, CAST(count(invoice_items.TrackId) as REAL) / CountryQuantityDate.CountryAmount , count(invoice_items.TrackId) as InvoiceCount, CountryQuantityDate.CountryAmount as CountryAmount
FROM invoice_items
JOIN invoices 
on invoices.InvoiceId = invoice_items.InvoiceId
join tracks
on tracks.trackId = invoice_items.trackId
join genres
on genres.genreId = tracks.genreId
join
(SELECT invoices.billingCountry as BillingCountry, sum(invoice_items.Quantity) as CountryAmount
FROM invoice_items
JOIN invoices 
on invoices.InvoiceId = invoice_items.InvoiceId
join tracks
on tracks.trackId = invoice_items.trackId
join genres
on genres.genreId = tracks.genreId
group by  invoices.billingCountry
) AS CountryQuantityDate
on CountryQuantityDate.BillingCountry = invoices.billingCountry
group by  invoices.billingCountry, tracks.genreId