我必须做一些关于日常用水量的任务。在数据库中,我有每小时和每天的水读数。我需要以某种方式提取每日消费的总量。我现在拥有的:
时间戳消耗
2013-07-28 00:00:32 143.720
2013-07-28 01:00:31 143.720
2013-07-28 02:00:32 143.720
2013-07-28 03:00:31 143.720
2013-07- 28 04:00:36 143.720
2013-07-28 05:00:31 143.720
2013-07-28 06:00:32 143.720
2013-07-28 07:00:32 143.720
2013-07-28 08:00:31 143.720
2013-07-28 09:00:32 143.720
2013-07-28 10:00:31 143.720
2013-07-28 11:00:31 143.720 2013-07-28
12:00:31 143.720 2018-03
13:00:31 143.720
2013-07-28 14:00:32 143.720
2013-07-28 15:00:31 143.720
2013-07-28 16:00:32 143.730
2013-07-28 17:00:31 143.73.
2013-07-28 18:00:31 143.730
2013-07-28 19:00:31 143.780
2013-07-28 20:00:31 143.820
2013-07-28 21:00:32 143.820
2013-07-28 22 :00:31 143.820
2013-07-28 23:00:32 143.820
2013-07-29 00:00:31 143.820
2013-07-29 01:00:32 143.820
2013-07-29 02:00:31
143.820 -07-29 03:00:32 143.820
2013-07-29 04:00:31 143.850
2013-07-29 05:00:32 143.850
2013-07-29 06:00:31 143.850
[我的一个数据]
所以基本上,我需要每天记录一条记录,然后用第二天的值减去这些值,依此类推......
例如
2013-07-28 00:00:32 143,720
2013-07-29 00:00:31 143,820
2013-07-29 06:00:31 143,850
绝对值(143,720 - 143,820)= 0,100
绝对值 (143,820 - 143,850) = 0,030
我希望输出为:
消费日期
2013-07-28 0,100
2013-07-29 0,030
所以就是每天用水量的差异
我当前的代码(获取我提到的这段数据):
SELECT water.timeStamp, water.volume1
FROM water
INNER JOIN meter
ON water.meterId=meter.id and water.timeStamp between date_add(curdate(), INTERVAL -31 DAY) and curdate()
INNER JOIN usercustomer
ON meter.customerId = usercustomer.customerId
INNER JOIN user
ON usercustomer.userId=user.id AND user.username LIKE 'user' AND user.password LIKE 'pass'
ORDER BY water.timeStamp ASC;
//有时每天的读数少于 24 个,而且可能不在同一分钟内
编辑 此外,即使当天还没有结束,我也希望有当天的消费。例如,如果有 00-06 的读数,我想显示它们。现在这里是一个使用 null 而不是最后一天用水量的示例http://sqlfiddle.com/#!2/979c1/1