Further fixes. Will rewrite sooN.

This commit is contained in:
Warkanum
2015-01-02 22:01:05 +02:00
parent c98df8c1da
commit e58bec2dfa
4 changed files with 144 additions and 94 deletions

View File

@@ -5,6 +5,8 @@ CREATE TABLE sensordata
data_read text,
time_read bigint,
time_changed bigint,
date_added date,
time_added time
CONSTRAINT pk_id PRIMARY KEY (id),
CONSTRAINT k_typeread UNIQUE (sensor_type, time_read)
)

View File

@@ -2,9 +2,8 @@
-- DROP VIEW v_sensordata_current;
CREATE OR REPLACE VIEW v_sensordata_current AS
SELECT r1.rn,
r1.id,
CREATE OR REPLACE VIEW v_sensordata_current_2 AS
SELECT r1.id,
r1.sensor_type,
r1.data_read,
to_timestamp(r1.time_read) AT TIME ZONE 'UTC' as time_read
@@ -16,3 +15,25 @@ CREATE OR REPLACE VIEW v_sensordata_current AS
sensordata.time_changed
FROM sensordata) r1
WHERE r1.rn = 1;
CREATE OR REPLACE VIEW v_sensordata_current AS
SELECT r1.id,
r1.sensor_type,
r1.data_read,
r1.time_read
FROM ( SELECT row_number() OVER (PARTITION BY r0.sensor_type ORDER BY r0.date_added DESC, r0.time_added DESC) AS rn,
r0.id,
r0.sensor_type,
r0.data_read,
(r0.date_added || ' ' || r0.time_added)::timestamp time_read
FROM (
select sc.*
from sensordata sc
where sc.date_added = now()::date
order by sc.id desc
limit 1000
) r0
) r1
WHERE r1.rn = 1;