MySQL Forums
Forum List  »  Newbie

Re: Calculating Delta and inserting into permanent table
Posted by: Rick James
Date: December 20, 2014 10:08AM

For a newbie, you figured out "self join" quite quickly. :)

A database is for raw data -- no deltas.
PHP is for embellishing on the data -- such as deltas.

Yes, deltas can be computed in SQL, but that leads to "redundant" data in the database; this is naughty. And the SQL to compute the deltas is messy.

OK, lecture over. Let's look at your question:
> Is it possible to insert 't2.colOdo' and 'delta' back into the tblGas as colOdo2 and colDelta?

Yes. See "multi-table UPDATE". That is, it is probably possible (painfully) to do the SELECT as part of an UPDATE.
Or, it should be possible to CREATE TEMPORARY TABLE .. SELECT. And then do a multi-table UPDATE to put the temp data back into the perm table. Again, painful.

When do you need the deltas? As you read the table? Then PHP is very good at computing that. Or...

See also @variables -- that would be a way for the SELECT to do some (all?) of the delta computation as it is being SELECTed from the table. This is much simpler. Perhaps:
SELECT colInd, colOdo, colOdo - @prev, @prev := colOdo
FROM ( SELECT @prev := 0 ) AS x
JOIN tblGas
ORDER BY colInd

Poof! Simpler code; no redundant stored permanently. (Sorry, I guess I am back to lecturing.)

Options: ReplyQuote


Subject
Written By
Posted
Re: Calculating Delta and inserting into permanent table
December 20, 2014 10:08AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.