Skip to content

Commit 527e945

Browse files
committed
UK Road Safty Accidents 2015 Project
1 parent 19d8903 commit 527e945

10 files changed

+398079
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
CREATE SCHEMA accidents;
2+
3+
USE accidents;
4+
5+
/* -------------------------------- */
6+
/* Create Tables */
7+
CREATE TABLE accident(
8+
accident_index VARCHAR(13),
9+
accident_severity INT
10+
);
11+
12+
CREATE TABLE vehicles(
13+
accident_index VARCHAR(13),
14+
vehicle_type VARCHAR(50)
15+
);
16+
17+
/* First: for vehicle types, create new csv by extracting data from Vehicle Type sheet from Road-Accident-Safety-Data-Guide.xls */
18+
CREATE TABLE vehicle_types(
19+
vehicle_code INT,
20+
vehicle_type VARCHAR(10)
21+
);
22+
23+
/* -------------------------------- */
24+
/* Load Data */
25+
LOAD DATA LOCAL INFILE 'C:\\Users\\Accidents_2015.csv'
26+
INTO TABLE accident
27+
FIELDS TERMINATED BY ','
28+
ENCLOSED BY '"'
29+
LINES TERMINATED BY '\n'
30+
IGNORE 1 LINES
31+
(@col1, @dummy, @dummy, @dummy, @dummy, @dummy, @col2, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
32+
SET accident_index=@col1, accident_severity=@col2;
33+
34+
35+
LOAD DATA LOCAL INFILE 'C:\\Users\\Vehicles_2015.csv'
36+
INTO TABLE vehicles
37+
FIELDS TERMINATED BY ','
38+
ENCLOSED BY '"'
39+
LINES TERMINATED BY '\n'
40+
IGNORE 1 LINES
41+
(@col1, @dummy, @col2, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)
42+
SET accident_index=@col1, vehicle_type=@col2;
43+
44+
45+
LOAD DATA LOCAL INFILE 'C:\\Users\\vehicle_types.csv'
46+
INTO TABLE vehicle_types
47+
FIELDS TERMINATED BY ','
48+
ENCLOSED BY '"'
49+
LINES TERMINATED BY '\n'
50+
IGNORE 1 LINES;
51+
52+
/* -------------------------------- */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE accidents_median(
2+
vehicle_types VARCHAR(100),
3+
severity INT
4+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# UK Road Safty 2015 data
2+
import pymysql
3+
4+
myConnection = pymysql.connect(
5+
host="localhost", user="root", password="root", db="accidents")
6+
7+
cur = myConnection.cursor()
8+
9+
cur.execute(
10+
"SELECT vehicle_type FROM vehicle_types WHERE vehicle_type LIKE '%torcycle%';")
11+
12+
cycle_list = cur.fetchall()
13+
14+
15+
selectSQL = ('''
16+
SELECT vt.vehicle_type, a.accident_severity
17+
FROM accident a
18+
JOIN vehicles v ON a.accident_index = v.accident_index
19+
JOIN vehicle_types vt ON v.vehicle_type = vt.vehicle_code
20+
WHERE vt.vehicle_type LIKE %s
21+
ORDER BY a.accident_severity;
22+
''')
23+
24+
25+
insert_SQL = ('''INSERT INTO accidents_median
26+
VALUES(%s, %s);''')
27+
28+
29+
for cycle in cycle_list:
30+
cur.execute(selectSQL, cycle[0])
31+
accidents = cur.fetchall()
32+
33+
# calculate median severity
34+
# divide the length of accidents /2 to find the median of accdients list
35+
quotient, remainder = divmod(len(accidents), 2)
36+
37+
if remainder:
38+
# meaning odds number of items in accidents list
39+
median_severity = accidents[quotient][1]
40+
else:
41+
# even numbers of items in accidents list
42+
median_severity = (accidents[quotient]
43+
[1] + accidents[quotient + 2][1]) / 2
44+
45+
print("finding Median Severity for ", cycle[0])
46+
47+
# insert the calculated median severity into table
48+
cur.execute(insert_SQL, (cycle[0], median_severity))
49+
50+
myConnection.commit()
51+
myConnection.close()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* SIDE NOTE: Compare performance of the query rows by using Explain Icon first (Before Indexing and After Indexing)*/
2+
3+
/* Create index on accident_index as it is using in both vehicles and accident tables and join clauses using indexes will perform faster */
4+
CREATE INDEX accident_index
5+
ON accident(accident_index);
6+
7+
CREATE INDEX accident_index
8+
ON vehicles(accident_index);
9+
10+
11+
/* get Accident Severity and Total Accidents per Vehicle Type */
12+
SELECT vt.vehicle_type AS 'Vehicle Type', a.accident_severity AS 'Severity', COUNT(vt.vehicle_type) AS 'Number of Accidents'
13+
FROM accident a
14+
JOIN vehicles v ON a.accident_index = v.accident_index
15+
JOIN vehicle_types vt ON v.vehicle_type = vt.vehicle_code
16+
GROUP BY 1
17+
ORDER BY 2,3;
18+
19+
/* Average Severity by vehicle type */
20+
SELECT vt.vehicle_type AS 'Vehicle Type', AVG(a.accident_severity) AS 'Average Severity', COUNT(vt.vehicle_type) AS 'Number of Accidents'
21+
FROM accident a
22+
JOIN vehicles v ON a.accident_index = v.accident_index
23+
JOIN vehicle_types vt ON v.vehicle_type = vt.vehicle_code
24+
GROUP BY 1
25+
ORDER BY 2,3;
26+
27+
28+
/* Average Severity and Total Accidents by Motorcyle */
29+
SELECT vt.vehicle_type AS 'Vehicle Type', AVG(a.accident_severity) AS 'Average Severity', COUNT(vt.vehicle_type) AS 'Number of Accidents'
30+
FROM accident a
31+
JOIN vehicles v ON a.accident_index = v.accident_index
32+
JOIN vehicle_types vt ON v.vehicle_type = vt.vehicle_code
33+
WHERE vt.vehicle_type LIKE '%otorcycle%'
34+
GROUP BY 1
35+
ORDER BY 2,3;

0 commit comments

Comments
 (0)