commit 5202a3a20c590ca7ab6c9d3296f25c32aaf38b71 Author: Brieuc Dubois Date: Wed Apr 24 10:38:32 2024 +0200 Initial commit - P3 diff --git a/P3/mondial-orig.db b/P3/mondial-orig.db new file mode 100644 index 0000000..765516c Binary files /dev/null and b/P3/mondial-orig.db differ diff --git a/P3/period1.db b/P3/period1.db new file mode 100644 index 0000000..80fb1b3 Binary files /dev/null and b/P3/period1.db differ diff --git a/P3/period2.db b/P3/period2.db new file mode 100644 index 0000000..5d4e17f Binary files /dev/null and b/P3/period2.db differ diff --git a/P3/period3.db b/P3/period3.db new file mode 100644 index 0000000..cc8035a Binary files /dev/null and b/P3/period3.db differ diff --git a/P3/queries/query1.sql b/P3/queries/query1.sql new file mode 100644 index 0000000..7269bcc --- /dev/null +++ b/P3/queries/query1.sql @@ -0,0 +1,17 @@ +-- Determine the 10 countries with the most purchases; list the names of these countries, as well as the number of purchases; sort the results in decreasing order of purchases. + +SELECT name, cnt +FROM ( + SELECT country, sum(qty) as cnt + FROM ( + SELECT province, sum(qty) as qty + FROM Purchases + GROUP BY province + ) + JOIN province ON province = province.ROWID + GROUP BY country + ORDER BY cnt DESC + LIMIT 10 +) +JOIN COUNTRY on country == code +; diff --git a/P3/queries/query10.sql b/P3/queries/query10.sql new file mode 100644 index 0000000..85205c5 --- /dev/null +++ b/P3/queries/query10.sql @@ -0,0 +1,24 @@ +-- List the top-10 of provinces for which the difference between the total number of items in purchases of the last 10 days and the preceding period of 10 days is the largest (i.e., the increase is large; we do not look for the largest decreases); list the full name of the province, the full name of the country, and the number of purchases in the last 10 days, as well as in the preceding period of 10 days; order the results by the size of the difference. Note that if there is no preceding period, the outcome is supposed to be empty. + + +-- Expected Heading: RELATION {province CHARACTER, country CHARACTER, qty_last10 INTEGER, qty_1020 INTEGER} +-- How do you treat the default case as showed in the instructions? + +SELECT province.name AS province, country.name AS country, qty_last10, (qty_last20-qty_last10) AS qty_1020 +FROM ( + SELECT province, SUM(qty) as qty_last10 + FROM Purchases + WHERE time > date((SELECT MAX(time) FROM Purchases),'-10 days') + GROUP BY province +) AS L +JOIN ( + SELECT province, SUM(qty) as qty_last20 + FROM Purchases + WHERE time > date((SELECT MAX(time) FROM Purchases),'-20 days') + GROUP BY province +) AS P ON L.province = P.province +JOIN province ON L.province = province.ROWID +JOIN country ON province.country = country.code +ORDER BY (2*qty_last10-qty_last20) DESC +LIMIT 10 +; diff --git a/P3/queries/query2.sql b/P3/queries/query2.sql new file mode 100644 index 0000000..9712ef6 --- /dev/null +++ b/P3/queries/query2.sql @@ -0,0 +1,6 @@ +-- Determine the total number of purchases for each product. List the number of the product, as well as the total number of purchases. + +SELECT product, sum(qty) as cnt +FROM Purchases +GROUP BY product +; diff --git a/P3/queries/query3.sql b/P3/queries/query3.sql new file mode 100644 index 0000000..90ef3eb --- /dev/null +++ b/P3/queries/query3.sql @@ -0,0 +1,42 @@ +-- 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. + + +SELECT name AS country, province, abs, prop +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 +; diff --git a/P3/queries/query4.sql b/P3/queries/query4.sql new file mode 100644 index 0000000..2903ec5 --- /dev/null +++ b/P3/queries/query4.sql @@ -0,0 +1,15 @@ +-- List for each continent and each possible number of items per purchase (in the range 1...10), the corresponding number of purchases. Only consider countries that are 100% located in their respective continents. + +-- How do you treat the default case as showed in the instructions? +-- Expected Heading: RELATION {continent CHARACTER, qty INTEGER, cnt INTEGER} +-- How do you treat the case where a country is splitted into two continents? + +SELECT continent.name AS continent, qty, COUNT(qty) AS cnt +FROM continent +JOIN Encompasses ON Encompasses.continent = continent.name AND Encompasses.percentage = 100.0 +JOIN country ON country.code = Encompasses.country +JOIN province ON province.country = country.code +JOIN Purchases ON Purchases.province = province.ROWID +WHERE qty <= 10 AND qty >= 1 +GROUP BY continent.name, qty +; diff --git a/P3/queries/query6.sql b/P3/queries/query6.sql new file mode 100644 index 0000000..eb9ea7c --- /dev/null +++ b/P3/queries/query6.sql @@ -0,0 +1,10 @@ +-- Determine the total number of purchases for each of the last ten days. List the date, as well as the total number of purchases. + +-- Expected Heading: RELATION {time INTEGER, cnt INTEGER} +-- How do you treat the default case as showed in the instructions? + +SELECT time, COUNT(*) AS cnt +FROM Purchases +where time > date((SELECT MAX(time) FROM Purchases),'-10 days') +GROUP BY time +; diff --git a/P3/queries/query7.sql b/P3/queries/query7.sql new file mode 100644 index 0000000..6750d70 --- /dev/null +++ b/P3/queries/query7.sql @@ -0,0 +1,7 @@ +-- Determine the identifiers of purchases performed in Brabant on the last day present in the database. + +SELECT Purchases.id AS ids +FROM Purchases +JOIN province ON province.ROWID = Purchases.province +WHERE time > date((SELECT MAX(time) FROM Purchases),'-1 days') AND province.name = 'Brabant' +; diff --git a/P3/queries/query8.sql b/P3/queries/query8.sql new file mode 100644 index 0000000..dc55428 --- /dev/null +++ b/P3/queries/query8.sql @@ -0,0 +1,7 @@ +-- Determine the identifiers of purchases performed in Vienna on the last day present in the database. + +SELECT Purchases.id AS ids +FROM Purchases +JOIN province ON province.ROWID = Purchases.province +WHERE time > date((SELECT MAX(time) FROM Purchases),'-1 days') AND province.name = 'Vienna' +; diff --git a/P3/queries/query9.sql b/P3/queries/query9.sql new file mode 100644 index 0000000..d3c25d8 --- /dev/null +++ b/P3/queries/query9.sql @@ -0,0 +1,15 @@ +-- List for each continent and each possible number of items per purchase (in the range 1...10), the corresponding number of purchases. Only consider countries that are 100% located in their respective continents. + +-- How do you treat the default case as showed in the instructions? +-- Expected Heading: RELATION {continent CHARACTER, qty INTEGER, cnt INTEGER} +-- How do you treat the case where a country is splitted into two continents? + +SELECT continent.name AS continent, qty, COUNT(qty) AS cnt +FROM continent +JOIN Encompasses ON Encompasses.continent = continent.name AND Encompasses.percentage = 100.0 +JOIN country ON country.code = Encompasses.country +JOIN province ON province.country = country.code +JOIN Purchases ON Purchases.province = province.ROWID +WHERE qty <= 10 AND qty >= 1 AND time > date((SELECT MAX(time) FROM Purchases),'-10 days') +GROUP BY continent.name, qty +; diff --git a/P3/text.pdf b/P3/text.pdf new file mode 100644 index 0000000..2ed487f Binary files /dev/null and b/P3/text.pdf differ diff --git a/P3/toy.db b/P3/toy.db new file mode 100644 index 0000000..f8e94e7 Binary files /dev/null and b/P3/toy.db differ