I'm developing an application retrieving data from a small number of sensors in an interval of 10 seconds. Those values are stored in a single-table sqlite database.
One use case of this application is to print the daily maxima of each sensor which is read.
I don't like the idea of issueing a MAX...GROUP BY day
like query every time I want to retrieve those values, so I thought of maintaining a second table containing thos daily maxima.
- Is this the appropriate way of performing such a task?
I tried writing such a trigger, but it didn't work. I'm somehow missing the ability to do the comparison among new value, and value for the current hour already in the db (which might be no value, if a new hour just started)
CREATE TRIGGER update_maxima AFTER INSERT ON tick BEGIN INSERT OR REPLACE INTO maxima (time, device_id, lW) VALUES ( strftime('%Y-%m-%d 00:00:00', 'now') , new.device_id, (select case when new.lW > (select lW from maxima where device_id = new.device_id AND time = strftime('%Y-%m-%d 00:00:00', 'now')) then new.lW else (select lW from maxima where device_id = new.device_id AND time = strftime('%Y-%m-%d 00:00:00', 'now')) end ) ); END