我正在尝试根据时间范围计算卡车里程的差异。 他们每15分钟左右在我们的数据库中报告他们的里程表。 我相信两个主要的列将是Unit_ID,Readingdate,里程表 这就是我现在所处的位置。
(SELECT ODOMETER
FROM ODOHIST O
WHERE O.UNIT_ID = T1.UNIT_ID
AND READINGDATE < READINGDATE - 21 DAYS
ORDER BY READINGDATE DESC FETCH FIRST ROW ONLY)
AS ODOMETER,
答案 0 :(得分:0)
不确定我是否完全遵循或拥有所有信息来解决它,但是这样的事情呢?
(SELECT min(ODOMETER)
FROM ODOHIST O
WHERE O.UNIT_ID = T1.UNIT_ID AND DATE = READINGDATE - 21)
AS MIN_ODOMETER,
(SELECT max(ODOMETER)
FROM ODOHIST O
WHERE O.UNIT_ID = T1.UNIT_ID AND DATE = READINGDATE)
AS MAX_ODOMETER
然后取两者之间的区别。