-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode_tutorial_intermediate.sql
More file actions
284 lines (225 loc) · 7.76 KB
/
mode_tutorial_intermediate.sql
File metadata and controls
284 lines (225 loc) · 7.76 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
-- 0: Putting it together
-- 1: SQL Aggregate Functions
-- 2: SQL COUNT
/*
COUNT(계산하고픈 열 이름): a SQL aggregate function for counting the number of rows in a particular column
*/
SELECT COUNT(*)
FROM tutorial.aapl_historical_stock_price;
SELECT COUNT(high)
FROM tutorial.aapl_historical_stock_price; -- COUNT(*)보다 개수가 적음 > null이 존재하기 때문 (null값을 제외하고 카운팅됨)
SELECT COUNT(low)
FROM tutorial.aapl_historical_stock_price;
/*
COUNT()는 숫자가 아닌 열에도 사용할 수 있음
*/
SELECT COUNT(date)
FROM tutorial.aapl_historical_stock_price;
/*
머릿글이 count라고 지정되는 것보단 별칭을 붙여주는게 확인하기 더 편함
*/
SELECT COUNT(date) AS count_of_date
FROM tutorial.aapl_historical_stock_price;
SELECT COUNT(date) AS "Count Of Date" -- 공백을 사용하는 경우엔 꼭 "" 붙여주기 ('' 안 됨) > 이 경우에만 "" 이고 나머지는 다 '' 붙임
FROM tutorial.aapl_historical_stock_price;
SELECT * FROM tutorial.aapl_historical_stock_price;
SELECT
COUNT(date) AS count_of_date,
COUNT(year) AS count_of_year,
COUNT(month) AS count_of_month,
COUNT(open) AS count_of_open,
COUNT(high) AS count_of_high,
COUNT(low) AS count_of_low,
COUNT(close) AS count_of_close,
COUNT(volume) AS count_of_volume,
COUNT(id) AS count_of_id
FROM tutorial.aapl_historical_stock_price;
-- 3: SQL SUM
/*
SUM: a SQL aggregate function. that totals the values in a given column (단 숫자만 가능/수직으로만 계산)
SUM은 null을 0으로 취급하기 때문에 null 걱정 안 해도 됨!
*/
SELECT SUM(volume)
FROM tutorial.aapl_historical_stock_price;
SELECT SUM(open)/COUNT(open) as avg_open
FROM tutorial.aapl_historical_stock_price;
-- 4: MIN/MAX
/*
MIN/MAX: SQL aggregation functions that return the lowest and highest values in a particular column
*/
SELECT MIN(volume) AS min_volume,
MAX(volume) AS max_volume
FROM tutorial.aapl_historical_stock_price;
SELECT MIN(low) AS min_price
FROM tutorial.aapl_historical_stock_price;
SELECT MAX(close - open)
FROM tutorial.aapl_historical_stock_price;
-- 5: AVG
/*
AVG: SQL aggregate function that calculates the average of a selected group of values
특징 1) 숫자 열에만 사용할 수 있음
특징 2) Null 값을 완전히 무시
*/
SELECT AVG(high)
FROM tutorial.aapl_historical_stock_price
WHERE high IS NOT NULL;
SELECT AVG(high)
FROM tutorial.aapl_historical_stock_price;
SELECT AVG(volume)
FROM tutorial.aapl_historical_stock_price;
-- 6: GROUP BY
/*
GROUP BY: SQL aggregate function allows you to separate data into groups, which can be aggregated independently of one another
*/
SELECT year,
COUNT(*) AS count
FROM tutorial.aapl_historical_stock_price
GROUP BY year;
SELECT year,
month,
COUNT(*) AS count
FROM tutorial.aapl_historical_stock_price
GROUP BY year, month;
SELECT year,
month,
SUM(volume) AS sum_volume
FROM tutorial.aapl_historical_stock_price
GROUP BY year, month
ORDER BY year, month;
SELECT year,
avg(close - open) as average
FROM tutorial.aapl_historical_stock_price
GROUP BY year
ORDER BY year;
SELECT month,
max(high) as highest,
min(low) as lowest
FROM tutorial.aapl_historical_stock_price
GROUP BY month
ORDER BY month;
-- 7: HAVING
/*
HAVING: the "clean" way to filter a query that has been aggregated
[ 쿼리 작성 순서 ]
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
*/
SELECT year,
month,
MAX(high) AS month_high
FROM tutorial.aapl_historical_stock_price
GROUP BY year, month
HAVING MAX(high) > 400
ORDER BY year, month;
-- 8: CASE
/*
The CASE statement is SQL's way of handling if/then logic
CASE WHEN ~ THEN ~ ELSE ~ END
여러 조건 중 OR 사용할 때 IN(,) 사용하면 더 편함
*/
SELECT * FROM benn.college_football_players;
SELECT player_name,
year,
CASE WHEN year = 'SR' THEN 'yes'
ELSE NULL END AS is_a_senior
FROM benn.college_football_players;
SELECT player_name,
year,
CASE WHEN year = 'SR' THEN 'yes'
ELSE 'no' END AS is_a_senior
FROM benn.college_football_players;
SELECT player_name,
state,
CASE WHEN state = 'CA' THEN 'yes'
ELSE NULL END AS from_cal
FROM benn.college_football_players
ORDER BY from_cal;
SELECT player_name,
weight,
CASE WHEN weight > 250 THEN 'over 250'
WHEN weight > 200 THEN '201-250'
WHEN weight > 175 THEN '176-200'
ELSE '175 or under' END AS weight_group
FROM benn.college_football_players;
SELECT player_name,
weight,
CASE WHEN weight > 250 THEN 'over 250'
WHEN weight > 200 AND weight <= 250 THEN '201-250'
WHEN weight > 175 AND weight <= 200 THEN '176-200'
ELSE '175 or under' END AS weight_group
FROM benn.college_football_players;
SELECT player_name,
height,
CASE WHEN height > 75 THEN 'over 75'
WHEN height > 70 AND height <= 75 THEN '70-75'
WHEN height > 65 AND height <= 70 THEN '65-70'
ELSE '65 or under' END AS height_group
FROM benn.college_football_players;
SELECT player_name,
CASE WHEN year = 'FR' AND position = 'WR' THEN 'frosh_wr'
ELSE NULL END AS sample_case_statement
FROM benn.college_football_players;
SELECT *,
CASE WHEN year IN('JR', 'SR') THEN player_name
ELSE NULL END AS jr_sr
FROM benn.college_football_players;
SELECT CASE WHEN year = 'FR' THEN 'FR'
ELSE 'Not FR' END AS year_group,
COUNT(*) AS count
FROM benn.college_football_players
GROUP BY year_group;
SELECT CASE WHEN year = 'FR' THEN 'FR'
WHEN year = 'SO' THEN 'SO'
WHEN year = 'JR' THEN 'JR'
WHEN year = 'SR' THEN 'SR'
ELSE 'No Year Data' END AS year_group,
COUNT(*) AS count
FROM benn.college_football_players
GROUP BY year_group;
SELECT CASE WHEN state IN ('CA','OR','WA') THEN 'West Coast'
WHEN state = 'TX' THEN 'Texas'
ELSE 'Others' END AS state_group,
COUNT(*) AS count
FROM benn.college_football_players
WHERE weight >= 300
GROUP BY state_group;
SELECT CASE WHEN year IN ('FR', 'SO') THEN 'underclass'
WHEN year IN ('JR', 'SR') THEN 'upperclass'
ELSE NULL END AS class_group,
SUM(weight) AS combined_player_weight
FROM benn.college_football_players
WHERE state = 'CA'
GROUP BY class_group;
-- 피벗팅
SELECT CASE WHEN year = 'FR' THEN 'FR'
WHEN year = 'SO' THEN 'SO'
WHEN year = 'JR' THEN 'JR'
WHEN year = 'SR' THEN 'SR'
ELSE 'No Year Data' END AS year_group,
COUNT(*) AS count
FROM benn.college_football_players
GROUP BY year_group;
SELECT COUNT(CASE WHEN year = 'FR' THEN 1 ELSE NULL END) AS fr_count,
COUNT(CASE WHEN year = 'SO' THEN 1 ELSE NULL END) AS so_count,
COUNT(CASE WHEN year = 'JR' THEN 1 ELSE NULL END) AS jr_count,
COUNT(CASE WHEN year = 'SR' THEN 1 ELSE NULL END) AS sr_count
FROM benn.college_football_players;
-- 행 * 열 둘다 원하는 기준이 있을 때
SELECT state,
COUNT(CASE WHEN year = 'FR' THEN 1 ELSE NULL END) AS fr_count, -- 열 피벗팅
COUNT(CASE WHEN year = 'SO' THEN 1 ELSE NULL END) AS so_count,
COUNT(CASE WHEN year = 'JR' THEN 1 ELSE NULL END) AS jr_count,
COUNT(CASE WHEN year = 'SR' THEN 1 ELSE NULL END) AS sr_count,
COUNT(*) AS total_players
FROM benn.college_football_players
GROUP BY state; -- 행 피벗팅
SELECT CASE WHEN school_name < 'n' THEN 'A-M'
WHEN school_name >= 'n' THEN 'N-Z'
ELSE NULL END AS school_name_group,
COUNT(*) AS players
FROM benn.college_football_players
GROUP BY school_name_group;