-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassn-4.sql
343 lines (220 loc) · 7.89 KB
/
assn-4.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/************************
*
* Assignment 4
* Meag Tessmann
* u1120583
* IS 6420 Database Design and Theory
* Submitted march 21, 2021
*
*************************/
-- 1: Create a query that uses a CASE statement and gets the following columns: order_id, num_items (number of items in the order), num_desks (number of desks in the order)
SELECT ol.order_id, sum(ol.quantity) num_items, sum(
CASE
WHEN ol.product_id = 104 THEN ol.quantity
ELSE 0
end
) num_desks
FROM order_line ol
group by ol.order_id
order by num_desks desc
;
-- 2: Create a query that uses a Common Table Expression (CTE) and a Window Function to get the following columns: state_province, last_customer_name (name of customer that placed an order most recently)
/*
WITH
latest_customer_cte as (
select distinct state_province, order_date, max(order_date) over() last_order_date, customer_name
from order_header oh
inner join customer c2
using (customer_id)
where order_date is not null
order by order_date desc
)
select state_province, customer_name
from latest_customer_cte
where order_date = last_order_date
;
*/
------ NOTE ------
-- choose to go with abbreviations since there were more of them.
WITH
state_customers_cte as (
select distinct state_province, order_date, max(order_date) over(partition by state_province) last_order_date, customer_name
from order_header oh
inner join customer c2
using (customer_id)
where order_date is not null
order by state_province desc
)
select state_province, customer_name as last_customer_name
from state_customers_cte
where order_date = last_order_date
and length(state_province) <3
order by state_province
;
/* public State GetStateByName(string name)
{
switch (name.ToUpper())
{
case "ALABAMA":
return State.AL;
case "ALASKA":
return State.AK;
case "AMERICAN SAMOA":
return State.AS;
case "ARIZONA":
return State.AZ;
case "ARKANSAS":
return State.AR;
case "CALIFORNIA":
return State.CA;
case "COLORADO":
return State.CO;
case "CONNECTICUT":
return State.CT;
case "DELAWARE":
return State.DE;
case "DISTRICT OF COLUMBIA":
return State.DC;
case "FEDERATED STATES OF MICRONESIA":
return State.FM;
case "FLORIDA":
return State.FL;
case "GEORGIA":
return State.GA;
case "GUAM":
return State.GU;
case "HAWAII":
return State.HI;
case "IDAHO":
return State.ID;
case "ILLINOIS":
return State.IL;
case "INDIANA":
return State.IN;
case "IOWA":
return State.IA;
case "KANSAS":
return State.KS;
case "KENTUCKY":
return State.KY;
case "LOUISIANA":
return State.LA;
case "MAINE":
return State.ME;
case "MARSHALL ISLANDS":
return State.MH;
case "MARYLAND":
return State.MD;
case "MASSACHUSETTS":
return State.MA;
case "MICHIGAN":
return State.MI;
case "MINNESOTA":
return State.MN;
case "MISSISSIPPI":
return State.MS;
case "MISSOURI":
return State.MO;
case "MONTANA":
return State.MT;
case "NEBRASKA":
return State.NE;
case "NEVADA":
return State.NV;
case "NEW HAMPSHIRE":
return State.NH;
case "NEW JERSEY":
return State.NJ;
case "NEW MEXICO":
return State.NM;
case "NEW YORK":
return State.NY;
case "NORTH CAROLINA":
return State.NC;
case "NORTH DAKOTA":
return State.ND;
case "NORTHERN MARIANA ISLANDS":
return State.MP;
case "OHIO":
return State.OH;
case "OKLAHOMA":
return State.OK;
case "OREGON":
return State.OR;
case "PALAU":
return State.PW;
case "PENNSYLVANIA":
return State.PA;
case "PUERTO RICO":
return State.PR;
case "RHODE ISLAND":
return State.RI;
case "SOUTH CAROLINA":
return State.SC;
case "SOUTH DAKOTA":
return State.SD;
case "TENNESSEE":
return State.TN;
case "TEXAS":
return State.TX;
case "UTAH":
return State.UT;
case "VERMONT":
return State.VT;
case "VIRGIN ISLANDS":
return State.VI;
case "VIRGINIA":
return State.VA;
case "WASHINGTON":
return State.WA;
case "WEST VIRGINIA":
return State.WV;
case "WISCONSIN":
return State.WI;
case "WYOMING":
return State.WY;
}
throw new Exception("Not Available");
}
*/
-- 3: Create the query from question #2 using a Temporary Table instead of a CTE. Please include a DROP IF EXISTS statement prior to your statement that creates the Temporary Table.
DROP TABLE IF EXISTS latest_customer_temp;
CREATE TEMP TABLE latest_customer_temp AS
select distinct state_province, order_date, max(order_date) over(partition by state_province) last_order_date, customer_name
from order_header oh
inner join customer c2
using (customer_id)
where order_date is not null
order by state_province desc
;
select state_province, customer_name as last_customer_name
from latest_customer_temp
where order_date = last_order_date
and length(state_province) <3
order by state_province
;
-- 4: Create the query from question #2 using a View instead of a CTE. Please include a DROP IF EXISTS statement prior to your statement that creates the View.
drop view if exists latest_customer_cte_view;
CREATE VIEW latest_customer_cte_view AS
select distinct state_province, order_date, max(order_date) over(partition by state_province) last_order_date, customer_name
from order_header oh
inner join customer c2
using (customer_id)
where order_date is not null
order by state_province desc
;
select state_province, customer_name as last_customer_name
from latest_customer_cte_view
where order_date = last_order_date
and length(state_province) <3
order by state_province
;
-- 5: Create a role named “product_administrator” with permissions to SELECT and INSERT records into the product table. Create a user named “bob finance” who is a member of that role.
drop role if exists product_administrator;
create role product_administrator;
create role bob_finance
login
password 'Abcd1234';
grant product_administrator to bob_finance;
grant select, insert on product to product_administrator;
-- revoke select on product from product_administrator;