From 6708584215b4142a379a5455ef9394b3181e97b7 Mon Sep 17 00:00:00 2001 From: Kjetil Skotheim Date: Sun, 16 Feb 2020 16:17:48 +0100 Subject: [PATCH] Commands to make a tiny SQLite database file of the data. Some SQL to get data. --- create-earthquake-sqlite.sh | 22 ++++++++++++++++++++++ earthquake-create.sql | 20 ++++++++++++++++++++ earthquake-report.sql | 13 +++++++++++++ 3 files changed, 55 insertions(+) create mode 100755 create-earthquake-sqlite.sh create mode 100644 earthquake-create.sql create mode 100644 earthquake-report.sql diff --git a/create-earthquake-sqlite.sh b/create-earthquake-sqlite.sh new file mode 100755 index 0000000..4a61b1e --- /dev/null +++ b/create-earthquake-sqlite.sh @@ -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 diff --git a/earthquake-create.sql b/earthquake-create.sql new file mode 100644 index 0000000..bb87bf8 --- /dev/null +++ b/earthquake-create.sql @@ -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); diff --git a/earthquake-report.sql b/earthquake-report.sql new file mode 100644 index 0000000..a370e23 --- /dev/null +++ b/earthquake-report.sql @@ -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; +