-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL code
180 lines (159 loc) · 3.55 KB
/
SQL code
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
-- Let's begin by calculating the average parts produced per year
-- I will use a subquery in the FROM clause to determine the total number of parts (this is to demonstrate my knowledge of subqueries - I would suggest using a CTE here)
SELECT
ROUND(AVG(total_parts),0) AS avg_yearly_parts_produced
FROM
-- calculating total parts produced
(SELECT
year,
SUM(num_parts) AS total_parts
FROM sets
GROUP BY year) parts_per_year;
-- Now lets group the previous query by year to see if annual production trends exist
SELECT
year,
ROUND(AVG(total_parts),0) AS avg_parts_per_year
FROM
-- calcuate total parts per year
(SELECT
year,
SUM(num_parts) AS total_parts
FROM sets
GROUP BY year) parts_per_year
GROUP BY year;
-- Next lets calculate the total number of sets produces
-- This requires the use of a JOIN
SELECT
COUNT(s.set_num) AS num_sets
FROM
themes as t
LEFT JOIN
sets as s
ON t.id = s.theme_id;
-- Now lets group the previous query by year to identify annual production trends
SELECT
year,
COUNT(set_num) AS sets
FROM sets
GROUP BY year
ORDER BY year;
-- To calculate the average sets produced per year over the span of the data set:
-- Create a CTE using the above query to find number of sets produced per year
-- Call the CTE in the FROM statement using the alias sets_per_yr
-- Average sets in SELECT statement to find avg_num_sets_year
WITH sets_per_yr AS
(SELECT
year,
COUNT(set_num) AS sets
FROM sets
GROUP BY year
Order By sets)
SELECT
Round(AVG(sets),2) as avg_num_sets_year
FROM sets_per_yr
-- Now we will look to see what the top 5 largest sets are
Select
DISTINCT name,
year,
num_parts
FROM sets
ORDER BY num_parts DESC
LIMIT 5;
-- Calculating the average sets per year from 1950-1997 then 1998-2017
WITH sets_per_yr AS
(SELECT
year,
COUNT(DISTINCT name) AS sets
FROM sets
GROUP BY year
Order By sets)
SELECT
ROUND(AVG(sets),2)
FROM sets_per_yr
WHERE
year < 1998;
WITH sets_per_yr AS
(SELECT
year,
COUNT(DISTINCT name) AS sets
FROM sets
GROUP BY year
Order By sets)
SELECT
AVG(sets)
FROM sets_per_yr
WHERE
year > 1997;
-- Lets look at set names during each time frame to see if there are any trends in sets
SELECT
t.name,
COUNT(s.set_num) AS num_sets
FROM
themes as t
LEFT JOIN
sets as s
ON t.id = s.theme_id
WHERE
s.year <= 1997
GROUP BY
t.name
ORDER BY
num_sets DESC;
SELECT
t.name,
COUNT(s.set_num) AS num_sets
FROM
themes as t
LEFT JOIN
sets as s
ON t.id = s.theme_id
WHERE
s.year > 1997
GROUP BY
t.name
ORDER BY
num_sets DESC;
-- Calculating the total number of Star Wars sets produced
SELECT
COUNT(s.set_num) AS num_star_wars_sets
FROM
themes as t
LEFT JOIN
sets as s
ON t.id = s.theme_id
WHERE
t.name ILIKE '%Star Wars%';
-- Finding number of Star Wars sets released per year
SELECT
DISTINCT year,
COUNT(s.set_num) OVER(PARTITION BY year) AS num_sets
FROM
themes as t
LEFT JOIN
sets as s
ON t.id = s.theme_id
WHERE
t.name ILIKE '%Star Wars%'
AND year IS NOT NULL
GROUP BY
year,
s.set_num
ORDER BY num_sets;
-- Lets look at the largest Star Wars set produced
SELECT
year,
t.name,
SUM(s.num_parts) AS parts
FROM
themes as t
LEFT JOIN
sets as s
ON t.id = s.theme_id
WHERE
t.name ILIKE '%Star Wars%'
GROUP BY
year,
t.name
HAVING
SUM(s.num_parts) IS NOT NULL
ORDER BY parts DESC;