是否有可能以某种方式存储一个值,然后在需要时使用它。也许您对以下问题有任何其他解决方案。我有每小时的水读数,我想将其表示为一天的消耗量,但有时可能会丢失一些读数。
SELECT Readings.dte AS day,
Ifnull(NextReadings.reading, (SELECT Max(consumption)
FROM meter_readings)) - Readings.reading AS Consumption
FROM (SELECT Date(readdate) AS dte,
Min(consumption) AS Reading
FROM meter_readings
GROUP BY Date(readdate)) AS Readings
LEFT JOIN
(SELECT Date(readdate) AS dte,
Min(consumption) AS Reading
FROM meter_readings
GROUP BY Date(readdate)) AS NextReadings
ON NextReadings.dte = date_add(Readings.dte, INTERVAL +1 DAY)
如果有
...
insert into meter_readings values ('2013-07-30 00:00:31', 143.860);
insert into meter_readings values ('2013-07-30 01:00:32', 143.870);
insert into meter_readings values ('2013-07-30 02:00:31', 143.870);
insert into meter_readings values ('2013-07-30 03:00:32', 143.870);
insert into meter_readings values ('2013-07-30 04:00:31', 143.870);
insert into meter_readings values ('2013-07-31 02:00:31', 143.890);
insert into meter_readings values ('2013-08-03 00:00:31', 143.900);
insert into meter_readings values ('2013-08-03 01:00:32', 143.920);
insert into meter_readings values ('2013-08-03 02:00:31', 143.920);
http://sqlfiddle.com/#!2/dd66cd/2 <--eg
如您所见,我:
July, 30 2013 0.03
July, 31 2013 0.03
August, 03 2013 0.02
的用法2013-07-31
is 0.03
和 that 不正确,因为SELECT Max(consumption) FROM meter_readings)
. 以某种方式存储最后一个正确值(143.870)然后使用而不是这个 Max(consumption)会很好。的用法2013-07-31
应该是:143.890 - Variable
变量是 143.870