1

我正在尝试执行以下查询,但我误用了聚合函数 sum()

询问

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

LogCat 输出

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)

有什么建议么 ?问候,兄弟

4

1 回答 1

2

您不能在单个查询中两次聚合值,但这可以通过子查询来解决。

尝试:

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)

备注:

  • 未经测试

  • 不确定查询的逻辑:您正在聚合值以便只保留一个条目,因此查询 category.name 没有意义

于 2014-09-22T15:59:47.417 回答