是否有可能以某种方式存储值,然后在需要时使用它。也许您对以下问题有任何其他解决方案。我有每小时的水读数,我想把它作为一天消费,但有时可能会丢失一些读数。
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< - 例如
我可以看到:
July, 30 2013 0.03
July, 31 2013 0.03
August, 03 2013 0.02
2013-07-31
的使用情况为0.03
,由于SELECT Max(consumption) FROM meter_readings)
而不是这样。以某种方式存储最后一个正确的值(143.870)然后使用而不是这个Max(消耗)将是很好的。 2013-07-31
的用法应为:143.890 - Variable
,其中变量为143.870
答案 0 :(得分:1)
我想你想拥有每一天的最后阅读。使用此语法来获取它:
SELECT M1.*
FROM meter_readings M1
LEFT JOIN meter_readings M2
ON Date(M1.ReadDate) = Date(M2.ReadDate)
AND M1.ReadDate < M2.ReadDate
WHERE M2.ReadDate IS NULL
或者每天读一读:
SELECT M1.*
FROM meter_readings M1
LEFT JOIN meter_readings M2
ON Date(M1.ReadDate) = Date(M2.ReadDate)
AND M1.ReadDate > M2.ReadDate
WHERE M2.ReadDate IS NULL
并获得当天的使用。我不太使用MySql。所以也许排名函数可以更好地设计。
SET @curRank1 = 0;
SET @curRank2 = 0;
SELECT
D1.ReadDate
,D1.Consumption AS D1Consumption
,D2.Consumption AS D2Consumption
,(D2.Consumption - D1.Consumption) AS UsageOfDay
FROM
(
SELECT M1.*, @curRank1 := @curRank1 + 1 AS rank
FROM meter_readings M1
LEFT JOIN meter_readings M2
ON Date(M1.ReadDate) = Date(M2.ReadDate)
AND M1.ReadDate < M2.ReadDate
WHERE M2.ReadDate IS NULL
) D2
LEFT JOIN
(
SELECT M1.*, @curRank2 := @curRank2 + 1 AS rank
FROM meter_readings M1
LEFT JOIN meter_readings M2
ON Date(M1.ReadDate) = Date(M2.ReadDate)
AND M1.ReadDate < M2.ReadDate
WHERE M2.ReadDate IS NULL
) D1
ON D2.rank = (D1.rank + 1)
答案 1 :(得分:0)
根据Wietze314的代码,我找到了解决方案。它有效,但它很重
SET @curRank1 = 0;
SET @curRank2 = 0;
SELECT
D1.ReadDate
,D1.Consumption AS D1Consumption
,D2.Consumption AS D2Consumption
,(D2.Consumption - D1.Consumption) AS UsageOfDay
FROM
(
SELECT M1.*, @curRank1 := @curRank1 + 1 AS rank
FROM meter_readings M1
LEFT JOIN meter_readings M2
ON Date(M1.ReadDate) = Date(M2.ReadDate)
AND M1.ReadDate > M2.ReadDate
WHERE M2.ReadDate IS NULL
) D2
LEFT JOIN
(
SELECT M1.*, @curRank2 := @curRank2 + 1 AS rank
FROM meter_readings M1
LEFT JOIN meter_readings M2
ON Date(M1.ReadDate) = Date(M2.ReadDate)
AND M1.ReadDate > M2.ReadDate
WHERE M2.ReadDate IS NULL
) D1
ON D2.rank = (D1.rank + 1)
WHERE D1.ReadDate IS NOT NULL
UNION ALL
(
SELECT DISTINCT
Date(My.ReadDate),
MIN(My.Consumption) AS D1Consumption,
MAX(My.Consumption)AS D2Consumption,
(MAX(My.Consumption) - MIN(My.Consumption)) AS UsageOfDay
FROM
meter_readings MY
GROUP BY Date(My.ReadDate) DESC
LIMIT 1
)