Recent

Author Topic: MAX function produces Error  (Read 1946 times)

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
MAX function produces Error
« on: February 26, 2015, 10:32:08 pm »
Can anyone tell a newbe why I get this error?

"Project raised exception class 'EDatabaseError' with message: no such column: max"

I have a table (called PAYMENTS), which gets updated with data from data aware controls on a form (called fmPayments).  Table PAYMENTS has an auto incremented Key field named PmtIdx, plus other fields that hold user input data.  fmPayments has a TSQLQuery and TDataSource on it.  TSQLite3Connection and TSQLTransaction are on a data module called GLOBAL.

When the Save button on form Payments is clicked, I want some of the fields from the just added record (i.e. with highest Key value) in PAYMENTS to be copied to the corresponding fields (some with different names) in a another table called LEDGER.  When I try to run the project, I get the error message at compile time.

I'm trying to use the MAX function in SQLite to select the most recent record in PAYMENTS.  Evidently my syntax is wrong.  Can anyone show me (with some example code) the best way to achieve my goal of wriing the most recent data from PAYMENTS into LEDGER?

I'm using Lazarus ver 1.2.6, FPC ver 2.6.4, SVN rev 46529,
i386-win32-win32/win64, sqlite-shell-win32-x86-3080704
Windows 10, Lazarus 2.2.2, FPC 3.2.2

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: MAX function produces Error
« Reply #1 on: February 26, 2015, 11:04:04 pm »
Apply MAX to a field like:
Quote
SELECT * from PAYMENTS where PmtIdx = (SELECT MAX(PmtIdx) from PAYMENTS)

Which could be achieved with one SELECT statement if you reverse the order and LIMIT the results to one record:
Quote
Select * From PAYMENTS Order By PmtIdx Desc LIMIT 1

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Re: MAX function produces Error
« Reply #2 on: February 27, 2015, 02:06:50 am »
engkin --
Thanks for the quick reply.  I used your second statement.  Works great!
Windows 10, Lazarus 2.2.2, FPC 3.2.2

 

TinyPortal © 2005-2018