-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duckdb.sql
88 lines (84 loc) · 1.82 KB
/
Duckdb.sql
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
-- Duckdb.sql
-- Create 'new_data' table to temporarily hold incoming data
DROP TABLE IF EXISTS new_data;
CREATE TABLE new_data (
when_captured VARCHAR,
device_urn VARCHAR,
device_sn VARCHAR,
device VARCHAR,
loc_name VARCHAR,
loc_country VARCHAR,
loc_lat DOUBLE,
loc_lon DOUBLE,
env_temp DOUBLE,
lnd_7318c VARCHAR,
lnd_7318u DOUBLE,
lnd_7128ec VARCHAR,
pms_pm02_5 VARCHAR,
bat_voltage VARCHAR,
dev_temp DOUBLE,
device_filename VARCHAR
);
-- Create 'measurements' table if it does not exist
CREATE TABLE IF NOT EXISTS measurements (
when_captured VARCHAR,
device_urn VARCHAR,
device_sn VARCHAR,
device VARCHAR,
loc_name VARCHAR,
loc_country VARCHAR,
loc_lat DOUBLE,
loc_lon DOUBLE,
env_temp DOUBLE,
lnd_7318c VARCHAR,
lnd_7318u DOUBLE,
lnd_7128ec VARCHAR,
pms_pm02_5 VARCHAR,
bat_voltage VARCHAR,
dev_temp DOUBLE,
device_filename VARCHAR
);
-- Load new data into 'new_data' table from JSON file
COPY new_data FROM 'last-24-hours.json' (FORMAT JSON);
-- Insert only unique new records into 'measurements' table
INSERT INTO measurements (
when_captured,
device_urn,
device_sn,
device,
loc_name,
loc_country,
loc_lat,
loc_lon,
env_temp,
lnd_7318c,
lnd_7318u,
lnd_7128ec,
pms_pm02_5,
bat_voltage,
dev_temp,
device_filename
)
SELECT DISTINCT
when_captured,
device_urn,
device_sn,
device,
loc_name,
loc_country,
loc_lat,
loc_lon,
env_temp,
lnd_7318c,
lnd_7318u,
lnd_7128ec,
pms_pm02_5,
bat_voltage,
dev_temp,
device_filename
FROM new_data
WHERE NOT EXISTS (
SELECT 1 FROM measurements
WHERE measurements.device_urn = new_data.device_urn
AND measurements.when_captured = new_data.when_captured
);