【问题标题】:Android Sqlite: android.database.sqlite.SQLiteException: aggregate functions are not allowed in the GROUP BY clauseAndroid Sqlite:android.database.sqlite.SQLiteException:GROUP BY 子句中不允许使用聚合函数
【发布时间】:2015-03-04 16:11:18
【问题描述】:

这里有一个奇怪的问题,查询在我桌面上的 Sqlite DB 浏览器中执行得很好 - 结果完全符合预期 - 但在 Android (4.4.2) 中执行光标时抛出异常。我对 Android 和 Sqlite 都很陌生,所以也许有一个“陷阱”我还没有发现。

简单查询:

select 
max(_id) as '_id', 
max(envid) as 'envid', 
max(action) as 'action', 
max(title) as 'title', 
max(cast(version as INTEGER)) as 'version', 
max(layout) as 'layout' 
from template  
where direction = 'return'  
group by title

例外:

E/AndroidRuntime(20731):由以下原因引起: android.database.sqlite.SQLiteException:聚合函数不是 在 GROUP BY 子句(代码 1)中允许:,编译时:select max(_id) 作为 '_id', max(envid) 作为 'envid', max(action) 作为 'action', max(title) as 'title', max(cast(version as INTEGER)) as 'version', max(layout) as 'layout' from template where direction = 'return' group 按标题

如果我将聚合函数放在 group by 子句中,我希望看到这一点……但我没有。

【问题讨论】:

  • 我想max() 是一个聚合函数。
  • 是的,在输入这​​个之后我想知道这个错误是否意味着在选择中使用了聚合。我把它缩减到这个并且它工作正常:“选择_id,envid,action,title,max(cast(version as INTEGER))作为'version',来自模板的布局,其中方向='return'按标题分组”这不是Android之外的问题,直接查询?谢谢!

标签: android sqlite group-by


【解决方案1】:

这解决了它 - 这是我真正需要的(参见上面 Rohit5k2 的评论。)

select _id, envid, action, title, max(cast(version as INTEGER)) as 'version', layout from template where direction = 'return' group by title

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 2012-12-07
    相关资源
    最近更新 更多