-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelpDesk.sql
220 lines (211 loc) · 3.72 KB
/
HelpDesk.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
##EASY
-- #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
i.call_date,
c.first_name,
c.last_name
FROM
Issue AS i
JOIN
Caller AS c
ON (c.caller_id = i.caller_id)
WHERE
c.first_name = 'Samantha'
AND c.last_name = 'Hall'
AND i.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 i
JOIN Staff sf
ON (i.Assigned_to = sf.Staff_code)
JOIN Level l
ON (sf.Level_code = l.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
s.Shift_date,
s.Shift_type,
sf.first_name,
sf.last_name
FROM
Shift s
JOIN
Staff sf
ON (s.Manager = sf.Staff_Code)
ORDER BY
sf.Shift_date;
-- #6
/*
List the Company name and the number of calls for those companies with more than 18 calls.
*/
SELECT
ct.Company_name,
c.COUNT(*)
FROM
Customer ct
JOIN
Caller c
ON (ct.Company_ref = c.Company_ref)
JOIN
Issue i
ON (c.Caller_id = i.Caller_id)
GROUP BY
ct.Company_name
HAVING
c.COUNT(*) > 18;
-- #7
/*
Find the callers who have never made a call. Show first name and last name
*/
SELECT
first_name,
last_name
FROM
Caller
WHERE caller_id NOT IN (SELECT caller_id FROM Issue)
-- #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,
Issue_Max.call_date
FROM
(
SELECT
b.call_date,
b.Taken_by,
b.Caller_id
FROM
(
SELECT
Issue.Caller_id,
MAX(Issue.call_date) AS call_date
FROM
Issue
GROUP BY
Issue.Caller_id
)
AS a
JOIN
Issue AS b
ON a.Caller_id = b.Caller_id
AND a.call_date = b.call_date
)
AS Issue_Max
JOIN
Staff
ON (Staff.Staff_code = Issue_Max.Taken_By)
JOIN
Caller
ON (Issue_Max.Caller_id = Caller.Caller_id)
WHERE
Caller.first_name = 'Harry';