1

我必须做一些关于日常用水量的任务。在数据库中,我有每小时和每天的水读数。我需要以某种方式提取每日消费的总量。我现在拥有的:

时间戳消耗
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

4

2 回答 2

2
select 
  Readings.dte as day, 
  PrevReadings.dte as prevday, 
  Readings.StartReading - PrevReadings.StartReading as consumption from 
(
  select Date(ReadDate) as dte, Min(Consumption) as StartReading 
  from meter_readings
  Group By Date(ReadDate)
) as Readings
left join
(
  select Date(ReadDate)as dte, Min(Consumption)  as StartReading
  from meter_readings
  Group By Date(ReadDate)
) as PrevReadings
on PrevReadings.dte = Readings.dte - 1

我假设您的目的是比较每天的开盘读数以计算每日消耗量。

http://sqlfiddle.com/#!2/819d1/9

希望有帮助

编辑 - 我添加了一个替代方案,可以澄清一些观点

http://sqlfiddle.com/#!2/819d1/11

您的编辑引入了一个新要求,可以使用以下 Sql 解决

    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 = Readings.dte + 1 
于 2013-08-28T15:58:04.683 回答
0

由于消耗量永远不会减少(一小时到下一小时),你能不能不服用

MAX(consumption) - MIN(consumption)

每个日期?

更新

你可能需要做

MIN(consumption_today) - MIN(consumption_yesterday)

包括并说明昨天最后一次阅读和今天第一次阅读之间发生的所有消费。

于 2013-08-28T13:23:33.547 回答