【问题标题】:Misuse of aggregate function MAX()滥用聚合函数 MAX()
【发布时间】:2015-05-19 17:13:55
【问题描述】:

我想要警报表中 ignition_status 列的 latest not null 值。

我有 unix_time 列,它是 Unix 时间戳中的时间,所以最大 unix_time 列值,最新的是条目。

下面是我的代码

cursor = dbUtilsObj.query(Alert.TABLE_NAME, new String[] { alertType_COLUMN }, " MAX(" + Alert.Columns.KEY_ALERT_UNIX_TIME
                + ")" + AND + Alert.Columns.KEY_MACHINE_TELE_DEVICE_NO + EQUALS + AND + alertType_COLUMN + IS_NOT_NULL,
                new String[] { String.valueOf(teleDevieNo) }, null, null, Alert.Columns.KEY_ALERT_UNIX_TIME);

出现错误

(1) misuse of aggregate function MAX()
: android.database.sqlite.SQLiteException: misuse of aggregate function MAX() (code 1): , while compiling: SELECT ignition_status FROM alert WHERE  MAX(unix_time) and tele_device_no = ?  and ignition_status IS NOT NULL  ORDER BY unix_time
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:686)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1420)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1267)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1138)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1306)
at com.jd.database.DBUtils.query(DBUtils.java:473)
at com.jd.database.AlertDBUtils.getLatestNotNullValueForType_COLUMN_ByTeleDeviceNo(AlertDBUtils.java:161)
at com.jd.sms.SMSIntentService.parseE1_VehicleMovemenEvent(SMSIntentService.java:158)
at com.jd.sms.SMSIntentService.parseMsgAndInputInDb(SMSIntentService.java:122)
at com.jd.sms.SMSIntentService.processMessage(SMSIntentService.java:97)
at com.jd.sms.SMSIntentService.procassRequest(SMSIntentService.java:71)
at com.jd.sms.SMSIntentService.onHandleIntent(SMSIntentService.java:39)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.os.HandlerThread.run(HandlerThread.java:60)
android.database.sqlite.SQLiteException: misuse of aggregate function MAX() (code 1): , while compiling: SELECT ignition_status FROM alert WHERE  MAX(unix_time) and tele_device_no = ?  and ignition_status IS NOT NULL  ORDER BY unix_time
  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)

简而言之,我正在尝试查询SELECT ignition_status FROM alert WHERE MAX(unix_time) and tele_device_no = ? and ignition_status IS NOT NULL ORDER BY unix_time

那么上面的dbUtilsObj.query()方法应该怎么做呢? 至少请告诉我正确的原始查询。

【问题讨论】:

  • 不能在 where 子句中使用聚合函数...
  • @germi 谢谢,它有帮助

标签: android sqlite max aggregate-functions select-query


【解决方案1】:

您不能在选择中使用像 MAX() 这样的聚合函数。

请考虑以下几点:

  • ORDER BY unixtime DESC 将与您的选择匹配的结果与最新的优先排序。

  • LIMIT 1 只返回第一个结果,即最新的。

【讨论】:

  • 目前您的回答已经消除了我的错误(异常),所以感谢并支持... :) 但我会检查它是否按我的意图在逻辑上工作,如果是,那么也会接受这个。
【解决方案2】:

在 WHERE CLUASE 中使用 MAX() 函数肯定是可能的,我亲自测试过。如下所示

SELECT ignition_status FROM alert
where 
unix_time=(Select MAX(unix_time) from alert)
and tele_device_no = ?  and ignition_status IS NOT NULL  ORDER BY unix_time

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多