Sqlite aggregate max(sum()) error in android

3.2k Views Asked by At

I am trying execute below query but I get misuse of aggregate function sum()

Query

select max(sum(entry.amount)),category.name from entry,category where entry.amount<0 and entry.cid=category.cid group by category.name  

LogCat Output

android.database.sqlite.SQLiteException: misuse of aggregate function sum() (code 1): , while  compiling: select sum(entry.amount),category.name from entry,category where entry.amount<0 and  entry.cid=category.cid group by category.name 09-22 20:20:18.626: E/AndroidRuntime(4093):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

Any suggestions ? regards, Brother

1

There are 1 best solutions below

4
On

You cannot aggregate values twice in a single query, but this can be solved with a subquery.

Try:

select max(amountsum)
from (select sum(entry.amount) as amountsum
      from entry,category
      where entry.amount<0
        and entry.cid=category.cid
      group by category.name)

Notes :

  • Untested

  • Not sure about the logic of the query: you are aggregating values so that only one entry remains, so querying category.name does not make sense