-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraffic_Offence_Database_SQL_Queries.sql
More file actions
382 lines (353 loc) · 12 KB
/
Traffic_Offence_Database_SQL_Queries.sql
File metadata and controls
382 lines (353 loc) · 12 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
/*
Traffic_Offence_Database_SQL_Queries
Author: Armin Berger
First created: 20/05/2020
Last edited: 10/06/2020
OVERVIEW:
This SQL script uses queries to retrieve required information on traffic offences from a relational database.
*/
/*
2(ii) Query 1
Show the demerit points and demerit description for all the demerits that either contains the word “heavy” or “Heavy” or
start with the word “Exceed” in the description. The column headings in your output should be renamed as Demerit Points
and Demerit Description. The output must be sorted in ascending format by demerit points and where two demerits have the
same points sort them in ascending format of demerit description. Your output must have the form shown below.
*/
SELECT
dem_points AS "Demerit Points",
dem_description AS "Demerit Description"
FROM
demerit
WHERE
dem_description LIKE '%heavy%'
OR dem_description LIKE '%Heavy%'
OR dem_description LIKE 'Exceed%'
ORDER BY
dem_points,
dem_description;
/*
2(ii) Query 2
For all “Range Rover” and “Range Rover Sport” models, show the main colour, VIN and manufacture year
for all the vehicles that were manufactured from 2012 to 2014. The column headings in your output should
be renamed as Main Colour, VIN and Year Manufactured. The output must be sorted by manufacture year in
descending format and where more than one vehicle was manufactured in the same year sort them by colour
in ascending format. Your output must have the form shown below.
*/
SELECT
veh_maincolor AS "Main Colour",
veh_vin AS "VIN",
to_char(veh_yrmanuf, 'YYYY') AS "Year Manufactured"
FROM
vehicle
WHERE
( veh_modname = 'Range Rover'
OR veh_modname = 'Range Rover Sport' )
AND to_char(veh_yrmanuf, 'YYYY') BETWEEN '2012' AND '2014'
ORDER BY
veh_yrmanuf DESC,
veh_maincolor;
/*
2(iii) Query 3
For all “Range Rover” and “Range Rover Sport” models, show the main colour, VIN and manufacture year for
all the vehicles that were manufactured from 2012 to 2014. The column headings in your output should be
renamed as Main Colour, VIN and Year Manufactured. The output must be sorted by manufacture year in descending
format and where more than one vehicle was manufactured in the same year sort them by colour in ascending format.
Your output must have the form shown below.
*/
SELECT
s.lic_no AS "Licence No.",
d.lic_fname
|| ' '
|| d.lic_lname AS "Driver Fullname",
to_char(d.lic_dob, 'DD-Mon-YYYY') AS "DOB",
d.lic_street
|| ' '
|| d.lic_town
|| ' '
|| d.lic_postcode AS "Driver Address",
to_char(s.sus_date, 'DD/MON/YY') AS "Suspended On",
to_char(s.sus_enddate, 'DD/MON/YY') AS "Suspended Till"
FROM
driver d
JOIN suspension s ON d.lic_no = s.lic_no
WHERE
to_date(s.sus_date, 'DD-MON-YYYY') BETWEEN to_date(add_months(current_date, - 30), 'DD-MON-YYYY') AND to_date(current_date, 'DD-MON-YYYY'
)
ORDER BY
s.lic_no,
s.sus_date DESC;
COMMIT;
/*
2(iv) Query 4
TDS would like to find out if there is any correlation between different months of a year and demerit codes so
you have been assigned to generate a report that shows for ALL the demerits, the code, description, total number
of offences committed for the demerit code so far in any month (of any year) and then the total of offences committed
for the demerit code in each month (of any year). The column headings in your output should be renamed as
Demerit Code, Demerit Description, Total Offences (All Months), and then the first three letters of each month
(with the first letter in uppercase). The output must be sorted by Total Offences (All Months) column in descending
format and where there is more than one demerit code with the same total, sort them by demerit code in ascending format.
Your output must have the form shown below. Your output can clearly be different from the following output.
*/
SELECT
dem_code AS "Demerit Code",
dem_description AS "Demerit Description",
COUNT(dem_code) AS "Total Offences(All Months)",
COUNT(jan) AS "Jan",
COUNT(feb) AS "Feb",
COUNT(mar) AS "Mar",
COUNT(apr) AS "Apr",
COUNT(may) AS "May",
COUNT(jun) AS "Jun",
COUNT(jul) AS "Jul",
COUNT(aug) AS "Aug",
COUNT(sep) AS "Sep",
COUNT(oct) AS "Oct",
COUNT(nov) AS "Nov",
COUNT(dec) AS "Dec"
FROM
(
SELECT
dem_code,
CASE to_char(off_datetime, 'MON')
WHEN 'JAN' THEN
+ 1
END AS jan,
CASE to_char(off_datetime, 'MON')
WHEN 'FEB' THEN
+ 1
END AS feb,
CASE to_char(off_datetime, 'MON')
WHEN 'MAR' THEN
+ 1
END AS mar,
CASE to_char(off_datetime, 'MON')
WHEN 'APR' THEN
+ 1
END AS apr,
CASE to_char(off_datetime, 'MON')
WHEN 'MAY' THEN
+ 1
END AS may,
CASE to_char(off_datetime, 'MON')
WHEN 'JUN' THEN
+ 1
END AS jun,
CASE to_char(off_datetime, 'MON')
WHEN 'JUL' THEN
+ 1
END AS jul,
CASE to_char(off_datetime, 'MON')
WHEN 'AUG' THEN
+ 1
END AS aug,
CASE to_char(off_datetime, 'MON')
WHEN 'SEP' THEN
+ 1
END AS sep,
CASE to_char(off_datetime, 'MON')
WHEN 'OCT' THEN
+ 1
END AS oct,
CASE to_char(off_datetime, 'MON')
WHEN 'NOV' THEN
+ 1
END AS nov,
CASE to_char(off_datetime, 'MON')
WHEN 'DEC' THEN
+ 1
END AS dec
FROM
offence
)
NATURAL JOIN demerit
GROUP BY
dem_code,
dem_description
ORDER BY
"Total Offences(All Months)" DESC,
"Demerit Code";
/*
2(v) Query 5
Find out which manufacturer's vehicles are involved in the highest number of offences which incur 2 or more demerit points.
Show the manufacturer name and the total number of offences that the manufacturer’s vehicles are involved in.
The column headings in your output should be renamed as Manufacturer Name and Total No. of Offences. The output must
be sorted by Total No. of Offences column in descending format and where there is more than one manufacturer
with the same total, sort them by manufacturer name in ascending format. Your output can clearly be different from the
following output.
*/
SELECT
veh_manufname AS "Manufacturer Name",
COUNT(off_no) AS "Total No. of Offences"
FROM
demerit
NATURAL JOIN ( offence o
JOIN vehicle v ON o.veh_vin = v.veh_vin )
WHERE
dem_points >= 2
GROUP BY
veh_manufname
HAVING
COUNT(off_no) = (
SELECT
MAX(COUNT(dem_code))
FROM
demerit
NATURAL JOIN ( offence o
JOIN vehicle v ON o.veh_vin = v.veh_vin )
WHERE
dem_points >= 2
GROUP BY
veh_manufname
)
ORDER BY
COUNT(o.off_no) DESC,
v.veh_manufname;
COMMIT;
/*
2(vi) Query 6
Find out the drivers who have been booked more than once for the same offence by an officer with the last name
as that of their last name. Show the driver licence number, driver full name (firstname and lastname together),
officer number, officer full name (firstname and lastname together). The column headings in your output should
be renamed as Licence No., Driver Name, Officer ID, Officer Name. The output must be sorted by
driver licence number column in ascending format.
*/
SELECT
lic_no AS "Licence No.",
lic_fname || ' '
|| lic_lname AS "Driver Name",
o.officer_id AS "Officer ID",
o.officer_fname
|| ' '
|| o.officer_lname AS "Officer Name"
FROM
driver
NATURAL JOIN ( offence offen
JOIN officer o ON offen.officer_id = o.officer_id )
GROUP BY
lic_no,
lic_fname,
lic_lname,
o.officer_id,
o.officer_fname,
o.officer_lname
HAVING lic_lname = officer_lname
AND COUNT(unique(offen.off_no)) <= 2
ORDER BY
lic_no;
/*
2(vii) Query 7
For each demerit code for which an offence has been recorded, find out the driver/s who has/have been booked the most
number of times. Show the demerit code, demerit description, driver licence number, driver full name
(firstname and lastname together) and the total number of times the driver has been booked in the output.
The column headings in your output should be renamed as Demerit Code, Demerit Description, Licence No., Driver Fullname
and Total Times Booked. The output must be sorted by demerit code in ascending format and where for one demerit
there is more than one driver booked the most number of times sort them by licence number in ascending format.
Your output must have the form shown below. Your output can clearly be different from the following output.
*/
SELECT
dem_code AS "Demerit Code",
dem_description AS "Demerit Description",
lic_no AS "Licence No.",
lic_fname
|| ' '
|| lic_lname AS "Driver Name",
COUNT(dem_code) AS "Total Times Booked"
FROM
driver
NATURAL JOIN ( offence
NATURAL JOIN demerit )
GROUP BY
dem_code,
dem_description,
lic_no,
lic_fname,
lic_lname
ORDER BY
dem_code,
lic_no;
/*
2(viii) Query 8
For each region, show the number of vehicles manufactured in the region and the percentage of vehicles manufactured
in the region. The last row of the output shows the totals - the second column which shows the total number of
vehicles manufactured in all regions (which is the total of all the individual totals in this column) and the
third column of which shows the total percentage of vehicles manufactured in all the regions (which is the total of
all the individual percentages in this column). The first character of the VIN represents the region where the vehicle
was manufactured.
*/
SELECT
region,
COUNT(veh_vin) AS "Total Vehicles Manufactured",
lpad(to_char(COUNT(veh_vin) /(
SELECT
COUNT(veh_vin)
FROM
vehicle
) * 100, 9999.99)
|| '%', 30, ' ') AS "Percentage of Vehicles Manufactured"
FROM
(
SELECT
veh_vin,
CASE
WHEN veh_vin BETWEEN 'A' AND 'D' THEN
'Africa'
WHEN veh_vin BETWEEN 'J' AND 'S' THEN
'Asia'
WHEN veh_vin > 'S' THEN
'Europe'
WHEN veh_vin BETWEEN '1' AND '6' THEN
'North America'
WHEN veh_vin BETWEEN '6' AND '8' THEN
'Oceania'
WHEN veh_vin > '8' THEN
'South America'
ELSE
'Unknown'
END AS region
FROM
vehicle
)
GROUP BY
region
UNION ALL
SELECT
nvl(null, 'Total'),
sum(veh_coun),
lpad(to_char(sum(veh_per) * 100, 9999.99)
|| ''
|| '%', 30, ' ')
FROM
(
SELECT
region,
COUNT(*) AS veh_coun,
round(COUNT(*) /(
SELECT
COUNT(*)
FROM
vehicle
), 4) AS veh_per
from(select
CASE
WHEN veh_vin BETWEEN 'A' AND 'D' THEN
'Africa'
WHEN veh_vin BETWEEN 'J' AND 'S' THEN
'Asia'
WHEN veh_vin > 'S' THEN
'Europe'
WHEN veh_vin BETWEEN '1' AND '6' THEN
'North America'
WHEN veh_vin BETWEEN '6' AND '8' THEN
'Oceania'
WHEN veh_vin > '8' THEN
'South America'
ELSE
'Unknown'
END AS region
FROM
vehicle
)
group by
region
)
;