Record events if values have changed in the last 3 days

亡梦爱人 提交于 2020-03-24 01:20:31

问题


I have a question, I have a table of importance. But the difference between these values is 3 days.

select 
    er.name,
    er.temp as real_temp, 
    der.temp as old_temp
from TEMPR_SILO er
    left join TEMPR_SILO der
    on er.name = der.name
where der.ID_TRANS in (select max(ID_TRANS) - 72 from TEMPR_SILO) 
    and er.ID_TRANS  in (select max(ID_TRANS) from TEMPR_SILO)
My answer
NAME	      REAL_TEMP	OLD_TEMP
SENSOR0001	7.98	    9.66
SENSOR0002	8.04	    9.91
SENSOR0003	7.91	    0.41
SENSOR0004	9.54	   -0.27
SENSOR0005	9.85	   -1.09
SENSOR0006	9.35	   -1.59
SENSOR0007	8.54	   -1.34

That is, in the answer I have the name of the sensor, and the temperature is real and old which is already 3 days. I have a question, can I make it so that if the temperature drops to 5, then I have a spare ALARM table that

 INSERT INTO ALARM ( NAME, INFO)    VALUES ( er.name ,'The temperature has greatly changed')
The table will have 600 values always. Thanks for the help) I want to implement this code in a background task that will be called every hour)

回答1:


You may add following query in your background task that will serve the purpose.

INSERT INTO IMS.ALARM (NAME,INFO)  
select er.name,'The temperature has significantly dropped'
from IMS.TEMPR_SILO er
    left join IMS.TEMPR_SILO der on er.name = der.name
where der.ID_TRANS in (select max(ID_TRANS) - 72 from IMS.TEMPR_SILO) 
and er.ID_TRANS  in (select max(ID_TRANS) from IMS.TEMPR_SILO)
and der.temp-er.temp>=6;

In my opinion, your solution can be little improved if you manage Alarming stuff in Trigger instead of some background process because there are some drawbacks in this way e.g.

  • Continuous background process will be doing hourly extra DB I/O in addition to consuming processing power

  • For any given reasons, if new records are not coming then program may register/insert new alarms on the basis of obsolete data

However, if you write a trigger 'ON INSERT' event on same table TEMPR_SILO by using above mentioned query then:

  • It will be executed automatically as and when new record gets inserted
  • False Alarms can be avoided
  • No background process will be required

I hope this solution will work for you.



来源:https://stackoverflow.com/questions/60503335/record-events-if-values-have-changed-in-the-last-3-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!