android中的Sqlite聚合最大值(sum())错误

时间:2014-09-22 15:37:45

标签: android sqlite misuse

我正在尝试执行以下查询,但我滥用了聚合函数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)

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

1 个答案:

答案 0 :(得分: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没有意义