Search code examples
mysqldelphitdbgridgeographic-distance

DBGrid - how to show the differnce between a column value in two adjacent rows as another column?


Let's say I record my car's latitude & longitude every minute or so, adding a row to a table each time.

I want to have a DB grid with 4 columns

  1. latitude
  2. longitude
  3. distance since last measurment
  4. curent street address, if known

Number 4, I can try to retrieve from Google Maps and I either get a text or blank, so let's ignore that.

How do I get #3? Should I calculate it in my application or in MySql (using a stored procedure or just a complicated SELECT)?

Whichever answer, can someone provide some sample code, or a link to an example? Google is not my friend today :-(


Solution

  • You can do it either way:

    • Calculate it in your query, and display it in a calculated column (such as when showing line totals where you're displaying ITEM_PRICE, QUANTITY, ITEM_PRICE * QUANTITY AS LINE_COST.

    • Calculate it in your application, using a calculated field (double-click the TTable component to open the Fields Editor, add field, set the type to ftCalculated, and write code in the OnCalcEvent for that calculated field.

    The first choice is usually preferable, because the DB is usually more efficient at doing those sorts of thing on sets of data than when your code does it by row instead. (Set operations are obviously more efficient than row operations.)