I'm a bit stuck at my Bachelor's thesis and hope you can help me. In order to evaluate a photovoltaic system I need to calculate the difference between total energy amounts. These are automatically updated in a MySQL-table with a timestamp, but without an id-number. I need to get the delta/difference between those energy amounts automatically as a extra column to visualize it in Grafana.
******************************************
Timestamp | SB1_AC_Total | Needed information (delta)
******************************************
2020-06-24 09:32:45 | 11.326.302 | 23
2020-06-24 09:32:02 | 11.326.279 | 22
2020-06-24 09:31:20 | 11.326.257 | ...
This list goes on for weeks. I really hope you can help me, because I have no idea and it is the first time I work with MySQL.
I was able to answer my question with the following code:
ALTER TABLE TABLENAME ADD SB1_AC_GES_DIFF INT;
DELIMITER $$
CREATE TRIGGER TABLENAME_Trigger
BEFORE INSERT
ON TABLENAME FOR EACH ROW
BEGIN
DECLARE SB1_AC_GES_old INT;
SELECT max(SB1_AC_GES) INTO SB1_AC_GES_old FROM TABLENAME;
SET NEW.SB1_AC_GES_Diff = New.SB1_AC_GES - SB1_AC_GES_old;
END$$
DELIMITER ;