-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoliceSQLQuery2.sql
More file actions
66 lines (61 loc) · 1.42 KB
/
Copy pathPoliceSQLQuery2.sql
File metadata and controls
66 lines (61 loc) · 1.42 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
/* For each state, compute the stolen gravity index
defined as the ratio between the total gravity of custodies
involving stolen guns divided by
the overall gravity of custodies. */
WITH TotalCG_Per_State AS (
SELECT
ge.state,
SUM(c.crime_gravity) AS TotalCG
FROM
Geography ge,
Custody c,
Gun gu
WHERE
ge.geo_id = c.geo_id AND
gu.gun_id = c.gun_id
GROUP BY
ge.state
),
TotalStolenGravity AS (
SELECT
ge.state,
sum(c.crime_gravity) AS TotalCG
FROM
Geography ge,
Custody c,
Gun gu
WHERE
ge.geo_id = c.geo_id AND
gu.gun_id = c.gun_id AND
gu.is_stolen = 'Stolen'
GROUP BY
ge.state
)
SELECT
t1.state,
ROUND(CAST(t2.TotalCG AS FLOAT) / t1.TotalCG, 2) AS StolenGravityIndex
FROM
TotalCG_Per_State t1,
TotalStolenGravity t2
WHERE
t1.state = t2.state
ORDER BY
StolenGravityIndex;
/* -------------- VERSION 2 ---------------- */
/* For each state, compute the stolen gravity index
defined as the ratio between the total gravity of custodies
involving stolen guns divided by
the overall gravity of custodies. */
SELECT
ge.state,
ROUND(SUM(CASE WHEN gu.is_stolen = 'Stolen' THEN CAST(c.crime_gravity AS FLOAT) ELSE 0 END)
/ SUM(c.crime_gravity), 5)
AS StolenGravityIndex
FROM
Custody c
INNER JOIN Geography ge ON c.geo_id = ge.geo_id
INNER JOIN Gun gu ON c.gun_id = gu.gun_id
GROUP BY
ge.state
ORDER BY
StolenGravityIndex;