-
Notifications
You must be signed in to change notification settings - Fork 0
/
USA_consumer_complaints_database.sqlite
258 lines (233 loc) · 7.52 KB
/
USA_consumer_complaints_database.sqlite
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
-- Quick looking at the table
select *
from consumer_complaints
limit 10;
-- Finding date range
select substr(date_received, 7, 4) as 'Year',
substr(date_received, 1, 2) as 'Month',
substr(date_received, 4, 2) as 'Day',
last_value (date_received) over (order by 1,2,3),
date_received
from consumer_complaints
order by 1, 2, 3;
-- Complaints by product (2011 - 2016), ranked
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
product,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
group by product
order by 3 desc;
-- Mortgage complains, ranked by issue (2011 - 2016)
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
issue,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
where product = 'Mortgage'
group by issue
order by 3 desc
limit 10;
-- Complaints by sub-product (2011 - 2016), ranked
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
sub_product,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
group by sub_product
order by 3 desc;
-- Complaints by company (2011 - 2016), ranked - Top 10
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
company,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
group by company
order by 3 desc
limit 10;
-- Complaints of Bank of America (2011 - 2016), ranked by product - Top 10
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
product,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
where company = 'Bank of America'
group by product
order by 3 desc
limit 10;
-- Complaints by state (2011 - 2016), ranked - Top 10
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
state,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
group by state
order by 3 desc
limit 10;
-- Finding most common products, and how much of total each one represents, in California
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
product,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
where state = 'CA'
group by product
order by 3 desc
limit 10;
-- Finding most common products, and how much of total each one represents, in New Jersey
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
product,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
where state = 'NJ'
group by product
order by 3 desc
limit 10;
-- Finding most common products, and how much of total each one represents, in Maryland
select distinct RANK() over (
order by count(*) desc
) as 'Rank',
product,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
where state = 'MD'
group by product
order by 3 desc
limit 10;
-- Finding the complaints distribuition over the years, between 2012 and 2015
select cast(substr(date_received, 7, 10) as integer) as 'Year',
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 1), 4) as 'percent. of total'
from consumer_complaints
where Year between 2012 and 2015
group by 1
order by 1;
-- Complaints by month (2012 - 2015) - Seasonality
select substr(date_received, 1, 2),
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
group by 1
order by 1;
-- Complaints over time, by product (Top 5) from 2012 to 2015
select distinct row_number() over (
partition by substr(date_received, 7, 10)
order by count(*) desc
) as 'Rank',
product,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total',
substr(date_received, 7, 10) as 'Year'
from consumer_complaints
group by Year, product
order by Year, 3 desc;
-- Complaints over time, by company (Top 5) from 2012 to 2015
select cast(substr(date_received, 7, 10) as integer) as 'Year',
company,
count(*) as 'number of complaints',
1.0 * (LEAD(count(*)) over(
partition by company
order by company) - count(*))/count(*) 'Change (in dec. points)'
from consumer_complaints
where (company = 'Equifax' or
company = 'Experian' OR
company = 'Bank of America' OR
company = 'JPMorgan Chase & Co.' or
company = 'Wells Fargo & Company') AND
(Year = 2012 or
Year = 2015)
group by Year, company
order by 2;
-- Complains by issue (2011 - 2016), from 'Equifax'
select distinct issue,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
where company = 'Equifax'
group by issue
order by 3 desc
limit 10;
-- Complains by issue (2011 - 2016), from 'Experian'
select distinct issue,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
where company = 'Experian'
group by issue
order by 3 desc
limit 10;
-- Complaints disputed by consumers over time, between 2012 and 2015
select cast(substr(date_received, 7, 10) as integer) as 'Year',
count(*) as 'number of complaints',
"consumer_disputed?"
from consumer_complaints
where Year between 2012 and 2015
group by 1, 3
order by 1;
-- Queries not used in the presentation:
-- Evolution of complaints throughout time, by company
select distinct row_number() over (
partition by substr(date_received, 7, 10)
order by count(*) desc
) as 'Rank',
company,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total',
substr(date_received, 7, 10) as 'Year'
from consumer_complaints
group by Year, company
order by Year, 3 desc;
-- Finding most common issues, and how much of total each one represents
select distinct issue,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
group by issue
order by 3 desc;
-- Finding most common sub-issues, and how much of total each one represents
select distinct sub_issue,
count(*) as 'number of complaints',
round(1.0 * count(*) / sum(count(*)) over (
order by 3), 4) as 'percent. of total'
from consumer_complaints
group by sub_issue
order by 3 desc;
-- Finding how many complaints were disputed by customers
select "consumer_disputed?",
count(*)
from consumer_complaints
group by 1;