This may be a pipe dream, but I am trying to get these 2 commands that work separately, to work as one single command. Instead of XXXXX, I would like to dump in the select statement (which gives me the difference of yesterday’s total and today’s total). The combined command would find the most recent row (max idtanklevel) and update it with the difference of itself and the prior row.
update fw_db.tanklevel2 T3
inner join (select max(idtanklevel) as idtanklevel from fw_db.tanklevel2) T4
on T3.idtanklevel = T4.idtanklevel
set glycolsmallchange = XXXXX;
and
select round(sum((T1.glycolsmall) - (T2.glycolsmall)),2)
from fw_db.tanklevel2 T1, fw_db.tanklevel2 T2
where
T1.idtanklevel = (select max(idtanklevel) from fw_db.tanklevel2)
and
T2.idtanklevel=(select max(idtanklevel)-1 from fw_db.tanklevel2);
Thanks in advance.
Here’s how I would get all that done in a single statement. (Note that this query assumes that
idtanklevelis the PRIMARY KEY or at least a UNIQUE KEY in thetanklevel2table; the query will need to be adjusted if that’s not the case.)Some brief comments to document the purpose of each table reference and each inline view:
Note that the query above uses a slightly different technique to identify the “prior” row. Rather than subtracting 1 from the maximum id value, the query gets the second highest id value (the highest id value that is lower than the maximum value).
To implement the same strategy your queries are using (i.e. subtracting 1 from the maximum id value, remove this portion of that query above:
and replace it with this:
To test this statement, without actually performing an update, remove the
SETclause and replace theUPDATEkeyword withSELECTand a list of relevant expressions that let you see the rows that are returned e.g.: