Query3 fix + query11/12

This commit is contained in:
Brieuc Dubois 2024-04-28 22:27:16 +02:00
parent 14dc4fe673
commit 0aa20f7f10
3 changed files with 57 additions and 35 deletions

21
P3/queries/query11.sql Normal file
View File

@ -0,0 +1,21 @@
DROP TABLE IF EXISTS ProductZeroPurchasesByProvince;
CREATE TABLE ProductZeroPurchasesByProvince AS
SELECT
c.name AS CountryName,
pro.name AS ProvinceName,
COUNT(pro.name) AS cnt
FROM
Purchases pur
JOIN
Province pro ON pur.product = 0 AND pur.province = pro.rowid
JOIN
Country c ON pro.country = c.code
GROUP BY
c.code, pro.name;
DROP INDEX IF EXISTS Province_index;
CREATE INDEX Province_index ON Province(name,country)
DROP INDEX IF EXISTS Country_index;
CREATE INDEX Country_index ON Country(code)

18
P3/queries/query12.sql Normal file
View File

@ -0,0 +1,18 @@
DROP TABLE IF EXISTS ProductZeroPurchasesByProvince;
CREATE TABLE ProductZeroPurchasesByProvince AS
SELECT
c.name AS CountryName,
pro.name AS ProvinceName,
COUNT(pro.name) AS cnt
FROM
Purchases pur
JOIN
Province pro ON pur.product = 0 AND pur.province = pro.rowid
JOIN
Country c ON pro.country = c.code
GROUP BY
c.code, pro.name;
DROP INDEX IF EXISTS Time_index;
CREATE INDEX Time_index ON Purchases(time,province,product,qty);

View File

@ -4,39 +4,22 @@
-- 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.
SELECT name AS country, province, abs, prop
SELECT
ProductZeroPurchasesByProvince.CountryName AS country,
ProductZeroPurchasesByProvince.ProvinceName AS province,
MaxCount AS maximum,
CAST(MaxCount AS FLOAT) / TotalCount AS proportion
FROM (
SELECT name AS province, country, max(cnt) as abs, max(cnt)*1.0/sum(cnt) AS prop
FROM(
SELECT province, sum(qty) as cnt
FROM Purchases
WHERE product = 0
GROUP BY province
)
JOIN Province ON province = Province.ROWID
GROUP BY country
)
JOIN Country ON country = code
;
-- V2
SELECT country.name, TMP.province, cnt AS abs, cnt*1.0/scnt AS prop
FROM
(
SELECT province.country AS code, province.name AS province, COUNT(Purchases.qty) AS cnt
FROM province
LEFT JOIN Purchases ON Purchases.product = 0 AND Purchases.province = province.ROWID
GROUP BY province.name
) AS TMP
JOIN (
SELECT code, province, MAX(cnt) AS mcnt, SUM(cnt) AS scnt FROM (
SELECT province.country AS code, province.name AS province, COUNT(Purchases.qty) AS cnt
FROM province
LEFT JOIN Purchases ON Purchases.product = 0 AND Purchases.province = province.ROWID
GROUP BY province.name
)
GROUP BY code
) AS F ON TMP.code = F.code AND TMP.cnt = F.mcnt
JOIN country ON country.code = TMP.code
;
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;