Initial commit - P3

This commit is contained in:
Brieuc Dubois 2024-04-24 10:38:32 +02:00
commit 5202a3a20c
15 changed files with 143 additions and 0 deletions

BIN
P3/mondial-orig.db Normal file

Binary file not shown.

BIN
P3/period1.db Normal file

Binary file not shown.

BIN
P3/period2.db Normal file

Binary file not shown.

BIN
P3/period3.db Normal file

Binary file not shown.

17
P3/queries/query1.sql Normal file
View File

@ -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
;

24
P3/queries/query10.sql Normal file
View File

@ -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
;

6
P3/queries/query2.sql Normal file
View File

@ -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
;

42
P3/queries/query3.sql Normal file
View File

@ -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
;

15
P3/queries/query4.sql Normal file
View File

@ -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
;

10
P3/queries/query6.sql Normal file
View File

@ -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
;

7
P3/queries/query7.sql Normal file
View File

@ -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'
;

7
P3/queries/query8.sql Normal file
View File

@ -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'
;

15
P3/queries/query9.sql Normal file
View File

@ -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
;

BIN
P3/text.pdf Normal file

Binary file not shown.

BIN
P3/toy.db Normal file

Binary file not shown.