linfo2172-databases/P3/queries/query3.sql

26 lines
1.2 KiB
MySQL
Raw Permalink Normal View History

2024-04-24 10:38:32 +02:00
-- Determine for each country the province in which the total number of purchases of product 0 is the highest; list the name of the country, the name of the province, and the number of purchases, both absolute and as a proportion of the total number of purchases in that country.
-- Expected Heading: RELATION {country CHARACTER, province CHARACTER, abs INTEGER, prop FLOAT}
-- How do you treat the default case as showed in the instructions?
-- How do you treat the case where several provinces of a same country have the same total number of purchases of product 0? You should show all such provinces.
2024-04-28 22:27:16 +02:00
SELECT
ProductZeroPurchasesByProvince.CountryName AS country,
ProductZeroPurchasesByProvince.ProvinceName AS province,
MaxCount AS maximum,
CAST(MaxCount AS FLOAT) / TotalCount AS proportion
2024-04-24 10:38:32 +02:00
FROM (
2024-04-28 22:27:16 +02:00
SELECT
CountryName AS CountryMax,
MAX(cnt) AS MaxCount,
SUM(cnt) AS TotalCount
FROM
ProductZeroPurchasesByProvince
GROUP BY
CountryName
) AS AggregatedData
JOIN
ProductZeroPurchasesByProvince ON AggregatedData.CountryMax = ProductZeroPurchasesByProvince.CountryName
WHERE
AggregatedData.MaxCount = ProductZeroPurchasesByProvince.cnt;