need help with PGSQL function for detecting temperature alerts. Let's imagine we have tables
**measurment_tab**
id:int
room_id:int
measurment_date:timestamp
value:long
**alert_tab**
id:int
room_id:int
alert_value:long
date_add:timestamp
last_check_date:timestamp
alert_type:varchar [two possible values: below, over]
Last measurement value is:
10|2|2020-01-02 15:33:00|11.3
and alert value:
33|2|12.7|2020-01-02 16:00:00|null|over
In this case function should detect alert when:
- measurement value is over 12.7
- each check I believe should updated last_check_date field to current value (now()) to limit the collection of values to check
I think this scenario is quite simple. Next one is more complicated.
Last measurement value is:
10|2|2020-01-02 15:33:00|13.3
and alert value:
33|2|12.7|2020-01-02 16:00:00|null|over
As You can see current measurement value is over alert value. In this case alert shouldn't be triggered. Alert should be triggered when:
- measurement value goes below alert value
- after that measurement goes again over alert value
Thanks for advice.