滥用聚合函数MAX()

Shi*_*ade 3 sqlite android max aggregate-functions select-query

我想在Alert表中列的最新非空ignition_status.

我的列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);
Run Code Online (Sandbox Code Playgroud)

得到错误

(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)
Run Code Online (Sandbox Code Playgroud)

总之,我正在尝试查询 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()上面的方法应该怎么做?至少请告诉我正确的原始查询.

laa*_*lto 5

您不能MAX()在选择中使用类似这样的聚合函数.

请考虑以下事项:

  • ORDER BY unixtime DESC 使用最新的选项对与您的选择匹配的结果进行排序.

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