SELECT
MAX(timestamp) as end,
MIN(timestamp) as start,
(MAX(odometerKM) - MIN(odometerKM)) as distanceTravelled,
( SELECT COUNT(*) FROM EventData WHERE speedKPH = 0 AND timestamp >= ? AND timestamp <= ? AND deviceID = ?) as stopsDuration,
( SELECT COUNT(*) FROM EventData WHERE speedKPH != 0 AND timestamp >= ? AND timestamp <= ? AND deviceID = ? ) as tripDuration,
(MAX(odometerKM) - MIN(odometerKM)) / ( SELECT fuelEconomy FROM Device WHERE deviceID = ?) as fuelConsumption
FROM EventData
WHERE deviceID = ? AND timestamp >= ? AND timestamp <= ?
相当混乱,但我创建了这个查询来查找一组行的摘要。最小和最大时间戳很简单,但要找到速度为零而不是零的行数,我做了一个丑陋的 hack。我确定有更好的方法来做到这一点?
更新:“?” 指代被 CodeIgniter 的一个不成熟的查询生成器所取代。遗憾的是,它还不支持命名参数。