-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryF.INITthenCMIT.sql
89 lines (89 loc) · 2.54 KB
/
QueryF.INITthenCMIT.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
SELECT DISTINCT
dbe.case_number
FROM
dscr_bail_events dbe
INNER JOIN
(
SELECT
cases.case_number
FROM
cases
WHERE
court IN
(
SELECT DISTINCT
court
FROM
cases
WHERE
query_court = 'BALTIMORE CITY'
)
)
AS ci
ON ci.case_number = dbe.case_number
INNER JOIN
dscr_events AS de
ON ci.case_number = de.case_number
WHERE
dbe.event_name = 'INIT'
AND dbe.DATE >= '2018-1-1'
AND dbe.DATE < '2018-1-31'
AND de.event_name = 'CMIT'
AND de.DATE >= dbe.DATE
AND de.DATE < '2018-2-1'
AND dbe.case_number NOT IN
(
SELECT DISTINCT
dbe.case_number
FROM
dscr_bail_events dbe
INNER JOIN
(
SELECT
cases.case_number
FROM
cases
WHERE
court IN
(
SELECT DISTINCT
court
FROM
cases
WHERE
query_court = 'BALTIMORE CITY'
)
)
AS ci
ON ci.case_number = dbe.case_number
INNER JOIN
dscr_bail_events balr
ON balr.case_number = ci.case_number
INNER JOIN
dscr_events AS rels
ON rels.case_number = ci.case_number
INNER JOIN
dscr_events AS bond
ON bond.case_number = ci.case_number
WHERE
dbe.event_name = 'INIT'
AND dbe.DATE >= '2018-1-1'
AND dbe.DATE < '2018.1.31'
AND rels.event_name = 'CMIT'
AND rels.DATE >= dbe.DATE
AND rels.DATE < '2018-2-1'
AND
(
(
bond.event_name = 'BOND'
AND bond.DATE >= dbe.DATE
AND bond.DATE < '2018-2-1'
)
OR
(
balr.event_name = 'BALR'
AND balr.DATE >= dbe.DATE
AND balr.DATE < '2018-2-1'
)
)
)