-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnlysis.sql
193 lines (125 loc) · 3.53 KB
/
Anlysis.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
use stolen_vehicles_db;
select * from stolen_vehicles;
-- data cleaning --
-- handling missing rows --
select * from stolen_vehicles
where vehicle_type="null";
-- turn off sql_safe Updates =0 and turn on keep 1
SET SQL_SAFE_UPDATES = 0;
delete from
stolen_vehicles
where
vehicle_type IS NULL;
-- CHECK FOR DUPLICATE ROWS--
WITH CTE AS
(
SELECT
*,
ROW_NUMBER() OVER(Partition by vehicle_id, vehicle_type, make_id, model_year, vehicle_desc, color, date_stolen, location_id ORDER BY vehicle_id ASC) as row_num
FROM stolen_vehicles
)
SELECT COUNT(*) FROM CTE WHERE row_num > 1;
-- EXTRACT YEAR, MONTH AND DAY NAME FROM COLUMN DATE_STOLEN AND MAKE A SEPARATE COLUMNS FOR EACH--
-- YEAR--
SELECT EXTRACT(Year FROM date_stolen) FROM stolen_vehicles;
ALTER TABLE stolen_vehicles
ADD COLUMN Year int after location_id;
UPDATE stolen_vehicles
SET Year = EXTRACT(Year FROM date_stolen);
-- MONTH--
SELECT EXTRACT(Month FROM date_stolen) FROM stolen_vehicles;
ALTER TABLE stolen_vehicles
ADD COLUMN Month int after Year;
UPDATE stolen_vehicles
SET Month = EXTRACT(Month FROM date_stolen);
-- WEEK--
SELECT EXTRACT(Week FROM date_stolen) FROM stolen_vehicles;
ALTER TABLE stolen_vehicles
ADD COLUMN Week int after Month;
UPDATE stolen_vehicles
SET Week = EXTRACT(Week FROM date_stolen);
-- DAY NAME--
SELECT Dayname(date_stolen) FROM stolen_vehicles;
ALTER TABLE stolen_vehicles
MODIFY COLUMN `Dayname` character(15) after Week;
UPDATE stolen_vehicles
SET `Dayname` = DAYNAME(date_stolen);
-- theft analysis on particular days --
SELECT
Year,
Month, Dayname,
COUNT(vehicle_id) AS theft_count
FROM
stolen_vehicles
GROUP BY
Year, Month ,Dayname
ORDER BY
Year ASC, Month ASC;
-- Stolen Vehicles Count by Region and Theft Rate Per Population --
SELECT
L.region,
L.country,
COUNT(V.vehicle_id) AS theft_count,
L.population,
(COUNT(V.vehicle_id) / L.population) * 1000 AS theft_rate_per_1000
FROM
stolen_vehicles_db.stolen_vehicles V
JOIN
stolen_vehicles_db.locations L ON V.location_id = L.location_id
GROUP BY
L.region, L.country, L.population
ORDER BY
theft_rate_per_1000 DESC;
-- Most Commonly Stolen Vehicle Makes, Models, and Colors --
SELECT
M.make_name,
M.make_type,
V.vehicle_type,
V.model_year,
V.color,
COUNT(V.vehicle_id) AS theft_count
FROM
stolen_vehicles_db.stolen_vehicles V
JOIN
stolen_vehicles_db.make_details M ON V.make_id = M.make_id
GROUP BY
M.make_name, M.make_type, V.vehicle_type, V.model_year, V.color
ORDER BY
theft_count DESC
LIMIT 10;
-- Calculate Vehicle Age at Time of Theft: --
SELECT
vehicle_id,
(Year - model_year) AS vehicle_age
FROM
stolen_vehicles_db.stolen_vehicles;
-- Identify High-Risk Vehicle Types in High-Risk Locations --
SELECT
L.region,
M.make_name,
V.vehicle_type,
COUNT(V.vehicle_id) AS theft_count
FROM
stolen_vehicles_db.stolen_vehicles V
JOIN
stolen_vehicles_db.make_details M ON V.make_id = M.make_id
JOIN
stolen_vehicles_db.locations L ON V.location_id = L.location_id
GROUP BY
L.region, M.make_name, V.vehicle_type
ORDER BY
theft_count DESC
LIMIT 10;
-- Monthly Trend of Vehicle Theft by Location: --
SELECT
L.region,
V.Month,
COUNT(V.vehicle_id) AS monthly_theft_count
FROM
stolen_vehicles_db.stolen_vehicles V
JOIN
stolen_vehicles_db.locations L ON V.location_id = L.location_id
GROUP BY
L.region, V.Month
ORDER BY
L.region, V.Month;