Skip to content

Instantly share code, notes, and snippets.

@mheadd
Last active August 23, 2016 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mheadd/a97ef8930ca70bbe4b1ef44b6c5e566f to your computer and use it in GitHub Desktop.
Save mheadd/a97ef8930ca70bbe4b1ef44b6c5e566f to your computer and use it in GitHub Desktop.
Steps to import Philly311 data into an SQLite DB to enable SQL queries

Here are some steps to bring the bulk Philly311 CSV file into a local SQLite database to enable SQL queries.

Tools you'll use

  • sqlite3 (installed by default on OS X - try typing in ~$ which sqlite3 at a command prompt).
  • csvsql (follow installation instructions here if not already installed).
  • SQLite Manager for Firefox.

Steps

Download the bulk CSV file for Philly311 requests. Note, there is a bug in the standard CSV download link. Use the alternate method described here.

Create a new sqlite table for the data:

~$ head -n 1000 311_Requests.csv | csvsql -i sqlite --tables requests | pbcopy
~$ sqlite3 philly311

Paste the table creation SQL code into the sqlite command prompt (cmd + v):

sqlite> CREATE TABLE requests (
  ...>	address VARCHAR(32), 
  ...>	agency_responsible VARCHAR(62), 
  ...>	expected_datetime DATETIME NOT NULL, 
  ...>	lat FLOAT, 
  ...>	lon FLOAT, 
  ...>	media_url VARCHAR(84), 
  ...>	requested_datetime DATETIME NOT NULL, 
  ...>	service_code VARCHAR(7), 
  ...>	service_name VARCHAR(38) NOT NULL, 
  ...>	service_notice VARCHAR(16), 
  ...>	service_request_id INTEGER NOT NULL, 
  ...>	shape VARCHAR(28), 
  ...>	status VARCHAR(6) NOT NULL, 
  ...>	updated_datetime DATETIME NOT NULL, 
  ...>	zipcode VARCHAR(10)
  ...>);

sqlite> .table
requests 

Note - you may also use csvsql to insert data from a csv directly into a database if preferred. See the docs for more info. if you want to use this method.

Execute the following commands to import data from the downloaded CSV to sqlite:

sqlite> .mode csv
sqlite> .import 311_Requests.csv requests
sqlite> .quit

Open Firefox and launch SQLite Manager. Open the philly311 database.

Now you can run SQL queries, like the following which shows average number of days to resolve a request for all closed requests by request category (see results below):

SELECT service_name, sum(1) as "num_requests", round(avg(julianday(updated_datetime) - julianday(requested_datetime)),2) 
AS 'resolved_time' from requests 
WHERE status = 'Closed' 
GROUP BY service_name 
ORDER BY resolved_time DESC; 
service_name num_requests resolved_time
Alley Light Outage 1196 120.76
No Heat Residential 7 117.29
Parks and Rec Safety and Maintenance 1416 96.59
Emergency Air Conditioning 2 96
Vacant House or Commercial 6639 92.09
No Heat (Residential) 1927 88.97
Smoke Detector 3623 78.23
Fire Residential or Commercial 1542 72.09
Abandoned Bike 57 70.13
Daycare Residential or Commercial 70 68.65
Hydrant Knocked Down (No Water) 27 67.07
Boarding Room House 714 64.29
Street Paving 757 63.94
Street Trees 2532 63.57
Zoning Business 2194 61.85
Maintenance Residential or Commercial 30577 61.5
Vacant Lot Clean-Up 9360 59.91
Building Construction 3684 58.54
License Residential 1470 57.06
Zoning Residential 1497 56.04
Construction Site Task Force 5442 54.42
Building Dangerous 3444 52.75
Infestation Residential 1040 50.15
Street Defect 20231 45.79
Line Striping 329 43.85
Other Dangerous 397 42.99
Other (Streets) 2412 41.34
Traffic (Other) 1852 40.29
Abandoned Vehicle 17982 40.1
Shoveling 1777 35.35
Newsstand Outdoor Cafe 14 33.95
Miscellaneous 3184 33.72
Graffiti Removal 16925 33.62
Dangerous Sidewalk 2125 32.26
Stop Sign Repair 1394 30.47
Hydrant Request 510 28.91
Manhole Cover 732 24.92
Complaint (Streets) 2596 23.71
Tree Dangerous 402 22.7
Complaints against Fire or EMS 15 18.8
Street Light Outage 12314 17.22
Illegal Dumping 23890 17.01
Inlet Cleaning 2342 12.48
Rubbish/Recyclable Material Collection 30870 10.98
Traffic Signal Emergency 6082 10.78
Sanitation / Dumpster Violation 6879 9.2
Dead Animal in Street 1549 7.68
Salting 14279 5.39
Police Complaint 44 4.41
Directory Assistance 25515 1.65
Information Request 673235 1.08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment