-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathschema.sql
68 lines (59 loc) · 2.1 KB
/
schema.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
-- drop table drone_event;
-- drop table drone_position;
-- Tables
CREATE TABLE drone_position (
id INT GENERATED ALWAYS AS IDENTITY,
name text not null,
location point not null,
latitude double precision not null,
longitude double precision not null,
destination point not null,
temp_celsius double precision not null,
battery_percent int not null,
battery_drain double precision not null,
speed int not null,
bearing double precision not null,
altitude double precision not null,
payload_percent int not null,
status text not null,
created timestamp with time zone default now()
);
CREATE TABLE drone_event (
id INT GENERATED ALWAYS AS IDENTITY,
event_type text not null,
message text not null,
name text not null,
hangar text not null,
warehouse text not null,
battery_percent int not null,
battery_consumed int not null,
location point not null,
latitude double precision not null,
longitude double precision not null,
payload int not null,
created timestamp with time zone default now()
);
-- Functions
-- drop function get_latest_positions;
CREATE or REPLACE FUNCTION get_latest_positions()
RETURNS TABLE(id int, name text, location point, destination point, temp_celsius double precision, battery_percent int, created timestamp with time zone)
AS
$$
BEGIN
RETURN QUERY SELECT tt.id,
tt.name,
tt.location,
tt.destination,
tt.temp_celsius,
tt.battery_percent,
tt.created
FROM drone_position tt
INNER JOIN
(SELECT dt.name, MAX(dt.created) AS MaxDateTime
FROM drone_position dt
GROUP BY dt.name) groupedtt
ON tt.name = groupedtt.name
AND tt.created = groupedtt.MaxDateTime;
END
$$ LANGUAGE 'plpgsql' VOLATILE;
-- select * from get_latest_positions();