diff options
author | yvesf <yvesf-git@xapek.org> | 2011-03-09 11:19:46 +0100 |
---|---|---|
committer | yvesf <yvesf-git@xapek.org> | 2011-03-09 11:19:46 +0100 |
commit | c15f4a600ddc3155b761230b84d78458e08bdc40 (patch) | |
tree | 48d924b516a86dd82fe0197a23045f418f286557 /trigger.sql | |
parent | 0e6c4ba0edfaeab0c1bf632dedb8dab46df1666d (diff) | |
parent | cfa235d91344cc6c8338d04f0e43dfeadedc1c72 (diff) | |
download | ebus-alt-c15f4a600ddc3155b761230b84d78458e08bdc40.tar.gz ebus-alt-c15f4a600ddc3155b761230b84d78458e08bdc40.zip |
Merge branch 'master' of ssh://xapek.org:2222/var/repos/ebus
Diffstat (limited to 'trigger.sql')
-rw-r--r-- | trigger.sql | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/trigger.sql b/trigger.sql index 0c06a99..fb60617 100644 --- a/trigger.sql +++ b/trigger.sql @@ -20,26 +20,26 @@ DROP FUNCTION value_insert_trigger_func() CASCADE; CREATE FUNCTION value_insert_trigger_func() RETURNS trigger AS $$ BEGIN - DELETE FROM value_cache - WHERE date_trunc('hour', timestamp) = - (SELECT date_trunc('hour', max(timestamp)) FROM value) + DELETE FROM value_cache + WHERE date_trunc('hour', timestamp) = + (SELECT date_trunc('hour', max(timestamp)) FROM value) AND sensor_id = (SELECT sensor_id from value where id = (select max(id) from value)); - INSERT INTO value_cache + INSERT INTO value_cache SELECT - COALESCE(0, (select max(id)+1 from value_cache)) as "id" --XXX use seq - ,date_trunc('hour', timestamp) AS "timestamp" - ,sensor_id AS "sensor_id" - ,type AS "type" - ,AVG(value_float) AS "value_float" - ,AVG(value_int) AS "value_int" - ,first(value_string) AS "value_string" + COALESCE(0, (select max(id)+1 from value_cache)) as "id" --XXX use seq + ,date_trunc('hour', timestamp) AS "timestamp" + ,sensor_id AS "sensor_id" + ,type AS "type" + ,AVG(value_float) AS "value_float" + ,AVG(value_int) AS "value_int" + ,first(value_string) AS "value_string" FROM value - WHERE date_trunc('hour', timestamp) = (SELECT date_trunc('hour', max(timestamp)) FROM value) - AND sensor_id = (SELECT sensor_id FROM value WHERE id = (SELECT MAX(id) FROM value)) - GROUP BY type, sensor_id, date_trunc('hour', timestamp); + WHERE date_trunc('hour', timestamp) = (SELECT date_trunc('hour', max(timestamp)) FROM value) + AND sensor_id = (SELECT sensor_id FROM value WHERE id = (SELECT MAX(id) FROM value)) + GROUP BY type, sensor_id, date_trunc('hour', timestamp); - RETURN NULL; + RETURN NULL; END; $$ LANGUAGE plpgsql; |