-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSQLZOO_HelpDesk_Answers.sql
More file actions
439 lines (423 loc) · 8.08 KB
/
SQLZOO_HelpDesk_Answers.sql
File metadata and controls
439 lines (423 loc) · 8.08 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
SQLZOO Helpdesk Answers
Questions available at http://sqlzoo.net/wiki/Help_Desk
*/
-- #1
/*
There are three issues that include the words "index" and "Oracle". Find the call_date for each of them
*/
SELECT
call_date,
call_ref
FROM
Issue
WHERE
detail LIKE '%index%'
AND detail LIKE '%Oracle%';
-- #2
/*
Samantha Hall made three calls on 2017-08-14. Show the date and time for each
*/
SELECT
Issue.call_date,
Caller.first_name,
Caller.last_name
FROM
Issue
JOIN
Caller
ON (Caller.caller_id = Issue.caller_id)
WHERE
Caller.first_name = 'Samantha'
AND Caller.last_name = 'Hall'
AND Issue.call_date LIKE '%2017-08-14%';
-- #3
/*
There are 500 calls in the system (roughly). Write a query that shows the number that have each status.
*/
SELECT
status,
Count(*) AS Volume
FROM
Issue
GROUP BY
status;
-- #4
/*
Calls are not normally assigned to a manager but it does happen. How many calls have been assigned to staff who are at Manager Level?
*/
SELECT
COUNT(*) AS mlcc
FROM
Issue
JOIN
Staff
ON (Issue.Assigned_to = Staff.Staff_code)
JOIN
Level
ON (Staff.Level_code = Level.Level_code)
WHERE
Level.Manager = 'Y';
-- #5
/*
Show the manager for each shift. Your output should include the shift date and type; also the first and last name of the manager.
*/
SELECT
Shift.Shift_date,
Shift.Shift_type,
Staff.first_name,
Staff.last_name
FROM
Shift
JOIN
Staff
ON (Shift.Manager = Staff.Staff_Code)
ORDER BY
Shift.Shift_date;
-- #6
/*
List the Company name and the number of calls for those companies with more than 18 calls.
*/
SELECT
Customer.Company_name,
COUNT(*)
FROM
Customer
JOIN
Caller
ON (Customer.Company_ref = Caller.Company_ref)
JOIN
Issue
ON (Caller.Caller_id = Issue.Caller_id)
GROUP BY
Customer.Company_name
HAVING
COUNT(*) > 18;
-- #7
/*
Find the callers who have never made a call. Show first name and last name
*/
SELECT
Caller.first_name,
Caller.last_name
FROM
Caller
LEFT JOIN
Issue
ON (Caller.Caller_id = Issue.Caller_id)
WHERE
Issue.Caller_id IS NULL;
-- #8
/*
For each customer show: Company name, contact name, number of calls where the number of calls is fewer than 5
*/
SELECT
a.Company_name,
b.first_name,
b.last_name,
a.nc
FROM
(
SELECT
Customer.Company_name,
Customer.Contact_id,
COUNT(*) AS nc
FROM
Customer
JOIN
Caller
ON (Customer.Company_ref = Caller.Company_ref)
JOIN
Issue
ON (Caller.Caller_id = Issue.Caller_id)
GROUP BY
Customer.Company_name,
Customer.Contact_id
HAVING
COUNT(*) < 5
)
AS a
JOIN
(
SELECT
*
FROM
Caller
)
AS b
ON (a.Contact_id = b.Caller_id);
-- #9
/*
For each shift show the number of staff assigned. Beware that some roles may be NULL and that the same person might have been assigned to multiple roles (The roles are 'Manager', 'Operator', 'Engineer1', 'Engineer2').
*/
SELECT
a.Shift_date,
a.Shift_type,
COUNT(DISTINCT role) AS cw
FROM
(
SELECT
shift_date,
shift_type,
Manager AS role
FROM
Shift
UNION ALL
SELECT
shift_date,
shift_type,
Operator AS role
FROM
Shift
UNION ALL
SELECT
shift_date,
shift_type,
Engineer1 AS role
FROM
Shift
UNION ALL
SELECT
shift_date,
shift_type,
Engineer2 AS role
FROM
Shift
)
AS a
GROUP BY
a.Shift_date,
a.Shift_type;
-- #10
/*
Caller 'Harry' claims that the operator who took his most recent call was abusive and insulting. Find out who took the call (full name) and when.
*/
SELECT
Staff.First_name,
Staff.Last_name,
a.Call_date
FROM
(
SELECT
Issue.Caller_id,
MAX(Call_date) AS Call_date
FROM
Issue
JOIN
Caller
ON (Issue.Caller_id = Caller.Caller_id)
WHERE
Caller.First_name = 'Harry'
GROUP BY
Issue.Caller_id
)
AS a
JOIN
Issue
ON (a.Caller_id = Issue.Caller_id
AND a.Call_date = Issue.Call_date)
JOIN
Staff
ON (Issue.Taken_by = Staff.Staff_code)
-- #11
/*
Show the manager and number of calls received for each hour of the day on 2017-08-12
*/
SELECT
Shift.Manager,
i.date_hour as Hr,
COUNT(*) as CC
FROM
(
SELECT
DATE_FORMAT(call_date, '%Y-%m-%d %H') date_hour,
DATE_FORMAT(call_date, '%Y-%m-%d') date,
DATE_FORMAT(call_date, '%H') hour,
Taken_by
FROM
Issue
WHERE
YEAR(call_date) = '2017'
AND MONTH(call_date) = '08'
AND DAY(call_date) = '12'
)
AS i
JOIN
Shift
ON (i.date = Shift.Shift_date)
WHERE
Shift.Shift_type = 'early'
AND i.hour <= 13
OR Shift.Shift_type = 'late'
AND i.hour > 13
GROUP BY
Shift.Manager,
i.date_hour
ORDER BY
i.date_hour
;
-- #12
/*
80/20 rule. It is said that 80% of the calls are generated by 20% of the callers. Is this true? What percentage of calls are generated by the most active 20% of callers.
*/
SELECT
ROUND(SUM(p2.cc / (SELECT COUNT(*) FROM Issue)) * 100, 4)
FROM
(
SELECT
p1.*,
@counter := @counter + 1 AS counter
FROM
(
SELECT
@counter := 0
)
AS initvar,
(
SELECT
Caller_id,
COUNT(*) AS cc
FROM
Issue
GROUP BY
Caller_id
ORDER BY
COUNT(*) DESC
)
AS p1
)
AS p2
WHERE
counter <= (20 / 100 * @counter);
-- #13
/*
Annoying customers. Customers who call in the last five minutes of a shift are annoying. Find the most active customer who has never been annoying.
*/
SELECT
Customer.Company_name,
COUNT(*)
FROM
Customer
JOIN
Caller
ON (Customer.Company_ref = Caller.Company_ref)
JOIN
Issue
ON (Caller.Caller_id = Issue.Caller_id)
WHERE
Customer.Company_name NOT IN
(
SELECT
Customer.Company_name
FROM
Customer
JOIN
Caller
ON (Customer.Company_ref = Caller.Company_ref)
JOIN
Issue
ON (Caller.Caller_id = Issue.Caller_id)
WHERE
(
DATE_FORMAT(call_date, '%H') = 13
OR DATE_FORMAT(call_date, '%H') = 19
)
AND DATE_FORMAT(call_date, '%i') >= 55
)
GROUP BY
Customer.Company_name
ORDER BY
COUNT(*) DESC LIMIT 1;
-- #14
/*
Maximal usage. If every caller registered with a customer makes a call in one day then that customer has "maximal usage" of the service. List the maximal customers for 2017-08-13.
*/
SELECT
a.Company_name,
a.caller_count,
b.issue_count
FROM
(
SELECT
Customer.Company_name,
COUNT(Caller.Company_ref) AS caller_count
FROM
Customer
JOIN
Caller
ON (Customer.Company_ref = Caller.Company_ref)
GROUP BY
Customer.Company_name
)
AS a
JOIN
(
SELECT
Customer.Company_name,
COUNT(DISTINCT Issue.Caller_id) AS issue_count
FROM
Customer
JOIN
Caller
ON (Customer.Company_ref = Caller.Company_ref)
JOIN
Issue
ON (Caller.Caller_id = Issue.Caller_id)
WHERE
YEAR(Issue.call_date) = '2017'
AND MONTH(Issue.call_date) = '08'
AND DAY(Issue.call_date) = '13'
GROUP BY
Customer.Company_name
)
AS b
ON a.Company_name = b.Company_name
WHERE
a.caller_count = b.issue_count;
-- #15
/*
Consecutive calls occur when an operator deals with two callers within 10 minutes. Find the longest sequence of consecutive calls – give the name of the operator and the first and last call date in the sequence.Is
*/
SELECT
a.taken_by,
a.first_call,
a.last_call,
a.call_count AS calls
FROM
(
SELECT
taken_by,
call_date AS last_call,
@row_number1:= CASE
WHEN
TIMESTAMPDIFF(MINUTE, @call_date, call_date) <= 10
THEN
@row_number1 + 1
ELSE
1
END AS call_count,
@first_call_date:= CASE
WHEN
@row_number1 = 1
THEN
call_date
ELSE
@first_call_date
END AS first_call,
@call_date:= Issue.call_date AS call_date
FROM
Issue,
(
SELECT
@row_number1 := 0,
@call_date := 0,
@first_call_date := 0
)
AS row_number_init
ORDER BY
taken_by,
call_date
)
AS a
ORDER BY
a.call_count DESC LIMIT 1;