-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlCommands.sql
133 lines (102 loc) · 3.25 KB
/
sqlCommands.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- SQL CODE FOR EXERCISE 02
CREATE TABLE MyDimDate (
dateid serial PRIMARY KEY,
date date NOT NULL,
year integer NOT NULL,
month integer NOT NULL,
monthname text NOT NULL,
day integer NOT NULL,
dayofweek integer NOT NULL,
dayname text NOT NULL,
quarter integer NOT NULL
);
CREATE TABLE MyDimWaste (
wasteid serial PRIMARY KEY,
waste_type text NOT NULL,
waste_description text,
waste_category text
);
CREATE TABLE MyDimZone (
zoneid serial PRIMARY KEY,
zone_name text NOT NULL,
city text NOT NULL,
country text
);
CREATE TABLE MyFactTrips (
trip_number serial PRIMARY KEY,
date_id integer NOT NULL,
waste_type_id integer NOT NULL,
zone_id integer NOT NULL,
waste_collected_tons numeric NOT NULL,
collection_station text
);
-- -----------------------------------------------------
-- SQL CODE FOR EXERCISE 03
CREATE TABLE DimDate (
dateid serial PRIMARY KEY,
date date NOT NULL,
year integer NOT NULL,
quarter integer NOT NULL,
quartername text NOT NULL,
month integer NOT NULL,
monthname text NOT NULL,
day integer NOT NULL,
weekday integer NOT NULL,
weekdayname text NOT NULL
);
CREATE TABLE DimTruck (
truckid serial PRIMARY KEY,
trucktype text NOT NULL
);
CREATE TABLE DimStation (
stationid serial PRIMARY KEY,
city text NOT NULL
);
CREATE TABLE FactTrips (
tripid serial PRIMARY KEY,
dateid integer NOT NULL,
stationid integer NOT NULL,
truckid integer NOT NULL,
wastecollected numeric NOT NULL
);
-- -----------------------------------------------------
-- SQL CODE FOR EXERCISE 04
-- creating a grouping sets query using the columns stationid, trucktype, and total waste collected
SELECT
stationid,
trucktype,
SUM(wastecollected) AS "Total Waste Collected"
FROM FactTrips
INNER JOIN DimTruck ON FactTrips.truckid = DimTruck.truckid
GROUP BY GROUPING SETS ((stationid, trucktype), (stationid), (trucktype));
-- creating a rollup query using the columns year, city, stationid, and total waste collected
SELECT
year,
city,
FactTrips.stationid,
SUM(wastecollected) AS "Total Waste Collected"
FROM FactTrips
INNER JOIN DimDate ON FactTrips.dateid = DimDate.dateid
INNER JOIN DimStation ON FactTrips.stationid = DimStation.stationid
GROUP BY ROLLUP (year, city, FactTrips.stationid);
-- creating a cube query using the columns year, city, stationid, and average waste collected
SELECT
year,
city,
FactTrips.stationid,
AVG(wastecollected) AS "Average Waste Collected"
FROM FactTrips
INNER JOIN DimDate ON FactTrips.dateid = DimDate.dateid
INNER JOIN DimStation ON FactTrips.stationid = DimStation.stationid
GROUP BY CUBE (year, city, FactTrips.stationid);
-- creating a Materialized Query Table (MQT) named max_waste_stats with the columns city, stationid, trucktype, and max waste collected
CREATE MATERIALIZED VIEW max_waste_stats AS
SELECT
DimStation.city,
FactTrips.stationid,
DimTruck.trucktype,
MAX(FactTrips.wastecollected) AS "Max Waste Collected"
FROM FactTrips
INNER JOIN DimStation ON FactTrips.stationid = DimStation.stationid
INNER JOIN DimTruck ON FactTrips.truckid = DimTruck.truckid
GROUP BY DimStation.city, FactTrips.stationid, DimTruck.trucktype;