From 0aa20f7f102a74390cbf8ba551dce53a1026b453 Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Sun, 28 Apr 2024 22:27:16 +0200 Subject: [PATCH] Query3 fix + query11/12 --- P3/queries/query11.sql | 21 +++++++++++++++++ P3/queries/query12.sql | 18 ++++++++++++++ P3/queries/query3.sql | 53 ++++++++++++++---------------------------- 3 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 P3/queries/query11.sql create mode 100644 P3/queries/query12.sql diff --git a/P3/queries/query11.sql b/P3/queries/query11.sql new file mode 100644 index 0000000..2d33ff6 --- /dev/null +++ b/P3/queries/query11.sql @@ -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) diff --git a/P3/queries/query12.sql b/P3/queries/query12.sql new file mode 100644 index 0000000..93ae1f8 --- /dev/null +++ b/P3/queries/query12.sql @@ -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); diff --git a/P3/queries/query3.sql b/P3/queries/query3.sql index 90ef3eb..a6e5e3f 100644 --- a/P3/queries/query3.sql +++ b/P3/queries/query3.sql @@ -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;