Room/SQLite 在一个查询中返回两个不同的结果
Room/SQLite returning two different results in one query
所以我有以下查询:
//returns min max value my odomoter field must have
@Query("SELECT MAX(odometer) FROM MaintenanceRecord WHERE vehicleId = :maintenanceVehicleId AND " +
"maintenanceTs < :dateAsLong")
Maybe<Float> getMinAllowedOdometer(long dateAsLong, int maintenanceVehicleId);
//returns the max value my odometer field is allowed to have
@Query("SELECT MIN(odometer) FROM MaintenanceRecord WHERE vehicleId = :maintenanceVehicleId AND " +
"CAST(strftime('%s',DATE(maintenanceTs/1000,'unixepoch')) AS integer) > " +
"CAST(strftime('%s',DATE(:dateAsLong/1000,'unixepoch')) AS integer)")
Maybe<Float> getMaxAllowedOdometer(long dateAsLong,int maintenanceVehicleId);
一个函数 returns odometer
列必须具有的最小值,另一个查询 returns odometer
列允许具有的最大值.
问题是 BOTH 函数是依次执行的,因为我需要订阅这两个函数。老实说,这是不好的做法。
我可以将这两个查询放在一个查询中,然后 return Maybe<Float, Float>
作为结果吗?
我的另一个解决方案是 运行 这些功能同步;而不是 Maybe<Float>,
我会直接 return Float.
您可以使用条件聚合在单个查询中获取两列:
SELECT MAX(CASE WHEN maintenanceTs < :dateAsLong THEN odometer END) AS max_odometer,
MIN(CASE WHEN CAST(strftime('%s', DATE(maintenanceTs / 1000, 'unixepoch')) AS INTEGER) > CAST(strftime('%s', DATE(:dateAsLong / 1000,'unixepoch')) AS INTEGER) THEN odometer END) AS min_odometer
FROM MaintenanceRecord
WHERE vehicleId = :maintenanceVehicleId;
所以我有以下查询:
//returns min max value my odomoter field must have
@Query("SELECT MAX(odometer) FROM MaintenanceRecord WHERE vehicleId = :maintenanceVehicleId AND " +
"maintenanceTs < :dateAsLong")
Maybe<Float> getMinAllowedOdometer(long dateAsLong, int maintenanceVehicleId);
//returns the max value my odometer field is allowed to have
@Query("SELECT MIN(odometer) FROM MaintenanceRecord WHERE vehicleId = :maintenanceVehicleId AND " +
"CAST(strftime('%s',DATE(maintenanceTs/1000,'unixepoch')) AS integer) > " +
"CAST(strftime('%s',DATE(:dateAsLong/1000,'unixepoch')) AS integer)")
Maybe<Float> getMaxAllowedOdometer(long dateAsLong,int maintenanceVehicleId);
一个函数 returns odometer
列必须具有的最小值,另一个查询 returns odometer
列允许具有的最大值.
问题是 BOTH 函数是依次执行的,因为我需要订阅这两个函数。老实说,这是不好的做法。
我可以将这两个查询放在一个查询中,然后 return Maybe<Float, Float>
作为结果吗?
我的另一个解决方案是 运行 这些功能同步;而不是 Maybe<Float>,
我会直接 return Float.
您可以使用条件聚合在单个查询中获取两列:
SELECT MAX(CASE WHEN maintenanceTs < :dateAsLong THEN odometer END) AS max_odometer,
MIN(CASE WHEN CAST(strftime('%s', DATE(maintenanceTs / 1000, 'unixepoch')) AS INTEGER) > CAST(strftime('%s', DATE(:dateAsLong / 1000,'unixepoch')) AS INTEGER) THEN odometer END) AS min_odometer
FROM MaintenanceRecord
WHERE vehicleId = :maintenanceVehicleId;