Skip to content
Snippets Groups Projects
Commit 6d8832f8 authored by pm3g19's avatar pm3g19
Browse files

Added relevant files

parents
No related tags found
No related merge requests found
README 0 → 100644
# COMP1204 Coursework
Some queries that I wrote for dataset.csv (a record of coronavirus cases) in SQL (SQLite3).
\ No newline at end of file
This diff is collapsed.
ex11.sql 0 → 100644
CREATE TABLE DateRep(
dateRep TEXT PRIMARY KEY NOT NULL,
day INTEGER NOT NULL,
month INTEGER NOT NULL,
year INTEGER NOT NULL
);
CREATE TABLE Country(
countryId INTEGER PRIMARY KEY NOT NULL,
countryterritoryCode CHAR(3),
popData2018 INTEGER,
continentExp VARCHAR(20)
);
CREATE TABLE GeoId(
countryId INTEGER PRIMARY KEY NOT NULL,
geoId VARCHAR(30) NOT NULL
);
CREATE TABLE CountryName(
countryId INTEGER PRIMARY KEY NOT NULL,
countriesAndTerritories VARCHAR(30) NOT NULL
);
CREATE TABLE Report(
dateRep TEXT NOT NULL,
countryId INTEGER NOT NULL,
cases INTEGER,
deaths INTEGER,
PRIMARY KEY (dateRep, countryId)
);
CREATE UNIQUE INDEX GeoId_geoId_index ON GeoId(geoId);
\ No newline at end of file
ex12.sql 0 → 100644
INSERT INTO DateRep(dateRep, day, month, year)
SELECT DISTINCT RawData.dateRep, RawData.day, RawData.month, RawData.year FROM RawData;
CREATE TEMPORARY TABLE CountryFull(
countryId INTEGER PRIMARY KEY AUTOINCREMENT,
countriesAndTerritories VARCHAR(30),
geoId VARCHAR(30),
countryterritoryCode CHAR(3),
popData2018 INTEGER,
continentExp VARCHAR(20)
);
INSERT INTO CountryFull(countriesAndTerritories, geoId, countryterritoryCode,popData2018,continentExp)
SELECT DISTINCT countriesAndTerritories, geoId, countryterritoryCode,popData2018,continentExp FROM RawData;
INSERT INTO Country(countryId, countryterritoryCode, popData2018, continentExp)
SELECT countryId, countryterritoryCode, popData2018, continentExp FROM CountryFull;
INSERT INTO CountryName(countryId, countriesAndTerritories)
SELECT countryId, countriesAndTerritories FROM CountryFull;
INSERT INTO GeoId(countryId, geoId)
SELECT countryId, geoId FROM CountryFull;
DROP TABLE CountryFull;
INSERT INTO Report(dateRep, countryId, cases, deaths)
SELECT dateRep, countryId, cases, deaths FROM RawData
NATURAL JOIN GeoId
\ No newline at end of file
SELECT SUM(cases) AS "total cases", SUM(deaths) as "total deaths" FROM Report;
\ No newline at end of file
SELECT cases, dateRep
FROM Report
NATURAL JOIN GeoId
NATURAL JOIN DateRep
WHERE geoId="UK"
ORDER BY year, month, day;
\ No newline at end of file
SELECT continentExp AS continent, dateRep AS "date", SUM(cases) AS cases, SUM(deaths) AS deaths
FROM Report
NATURAL JOIN Country
NATURAL JOIN DateRep
GROUP BY dateRep, continentExp
ORDER BY year, month, day;
\ No newline at end of file
SELECT countriesAndTerritories AS country, cast(SUM(cases)*100 AS real) / popData2018 AS "% cases of population", cast(sum(deaths)*100 AS real) / popData2018 AS "% deaths of population" FROM Report
NATURAL JOIN Country
NATURAL JOIN CountryName
GROUP BY countryId;
\ No newline at end of file
SELECT countriesAndTerritories AS "country name", ((CAST(sum(deaths) AS REAL) * 100) / sum(cases)) AS perc
FROM Report
NATURAL JOIN Country
NATURAL JOIN CountryName
GROUP BY countryId
ORDER BY perc DESC
LIMIT 10;
\ No newline at end of file
SELECT dateRep AS "date", sum(deaths) OVER win1 AS "cumulative UK deaths", sum(cases) OVER win1 AS "cumulative UK cases"
FROM Report
NATURAL JOIN DateRep
NATURAL JOIN GeoId
WHERE geoId="UK"
WINDOW win1 AS (ORDER BY year, month, day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment