From 1ec5023c53a7f479db0efe136d86f4f051ead5cf Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 13 Mar 2021 19:13:50 +0700 Subject: [PATCH] some example postgresql commands in use --- postgres/graph.sql | 37 ++++++++++++++++++++++++++++++++ postgres/setup.sql | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 postgres/graph.sql create mode 100644 postgres/setup.sql diff --git a/postgres/graph.sql b/postgres/graph.sql new file mode 100644 index 0000000..5aaf2d1 --- /dev/null +++ b/postgres/graph.sql @@ -0,0 +1,37 @@ +-- get data for time span from now +-- where epoch_time value is used for filtering +SELECT + epoch_time, aqi_value + FROM aqi + WHERE epoch_time > 1613975843 + ORDER BY epoch_time DESC; + + +-- get data from yesterday +SELECT + epoch_time, aqi_value + FROM aqi + WHERE epoch_time > 1613926800 + AND epoch_time < 1614013200 + ORDER BY epoch_time DESC + LIMIT 30 * 24; + + +-- last 7 days +SELECT + epoch_time, aqi_value + FROM aqi + WHERE epoch_time > 1613494800 + AND epoch_time < 1614099600 + ORDER BY epoch_time DESC + LIMIT 30 * 24 * 7; + + +-- last 48h of pm2.5 and pm10 values +SELECT + epoch_time, pm25, pm10 + FROM aqi + WHERE epoch_time < 1614963600 + AND epoch_time > 1614790800 + ORDER BY epoch_time DESC + LIMIT 30 * 48; diff --git a/postgres/setup.sql b/postgres/setup.sql new file mode 100644 index 0000000..55d34a5 --- /dev/null +++ b/postgres/setup.sql @@ -0,0 +1,53 @@ +-- create aqi table +CREATE TABLE aqi ( + epoch_time INT NOT NULL PRIMARY KEY, + time_stamp VARCHAR(20) NOT NULL, + uptime INT NOT NULL, + pm25 FLOAT4 NOT NULL, + pm10 FLOAT4 NOT NULL, + aqi_value FLOAT4 NOT NULL, + aqi_category VARCHAR(40) NOT NULL +); + +-- example aqi insert +INSERT INTO aqi ( + epoch_time, + time_stamp, + uptime, + pm25, + pm10, + aqi_value, + aqi_category +) VALUES ( + 1613648178, '2021-02-18 18:36:18', 206728, 20.4, 22.8, 67.0, 'Moderate' +); + + +-- create weather table +CREATE TABLE weather ( + epoch_time INT NOT NULL PRIMARY KEY, + time_stamp VARCHAR(20) NOT NULL, + temperature FLOAT4 NOT NULL, + pressure FLOAT4 NOT NULL, + humidity FLOAT4 NOT NULL, + wind_speed FLOAT4 NOT NULL, + wind_direction INT2 NOT NULL, + weather_name VARCHAR(29) NOT NULL, + weather_icon VARCHAR(3) NOT NULL +); + +-- example weather insert +INSERT INTO weather ( + epoch_time, + time_stamp, + temperature, + pressure, + humidity, + wind_speed, + wind_direction, + weather_name, + weather_icon +) VALUES ( + 1613648178, '2021-02-18 18:36:18', 27.32, 982.41, + 39.62598, 3.09, 40, 'Clouds', '04n' +);