Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands to make a tiny SQLite database file of the data. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions create-earthquake-sqlite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

function csv2insert { perl -plE 's,\cM,,;s,\x27,$&$&,g;$_="insert into quake values (".s/"(.*?)"/push@a,$1;"§§$#a"/egr=~s,(^|$),\x27,gr=~s/,/\x27,\x27/gr=~s/§§(\d+)/$a[$1]/gr.");"'; }

c=cat
if hash pv; then
c=pv
fi

if [ ! -f earthquake.csv ]; then
wget -N https://raw.githubusercontent.com/socratica/data/master/earthquake.csv
fi

cat earthquake.csv | tail -n+2 | csv2insert > earthquake-insert.sql
perl -i -ple '$.-1or$_="BEGIN;\n$_";$_.="\nEND;" if eof' earthquake-insert.sql


cat earthquake-create.sql | sqlite3 earthquake.sqlite
$c earthquake-insert.sql | sqlite3 earthquake.sqlite
ls -l earthquake.sqlite

echo "select 'earthquakes', sum(1) from quake;" | sqlite3 earthquake.sqlite
20 changes: 20 additions & 0 deletions earthquake-create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
drop table if exists quake;

create table quake (
earthquake_id integer,
occurred_on timestamp,
latitude real,
longitude real,
depth real,
magnitude real,
calculation_method varchar(128),
network_id varchar(128),
place varchar(500),
cause varchar(128),
CONSTRAINT earthquake_pkey PRIMARY KEY (earthquake_id)
);

create index i_quake_latitude on quake(latitude);
create index i_quake_longitude on quake(longitude);
create index i_quake_occurred_on on quake(occurred_on);
create index i_quake_magnitude on quake(magnitude);
13 changes: 13 additions & 0 deletions earthquake-report.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

with q as (select *,substr(occurred_on,1,4) year from quake)
select year, cause, sum(1) from q group by year, cause order by 1,2;

select * from quake where magnitude >= 8.0
order by magnitude, occurred_on;

select substr(occurred_on,1,4) year, sum(1)
from quake
where magnitude >= 7.5
group by substr(occurred_on,1,4)
order by 1;