SQL - 每日用水量。天之间的差异

时间:2013-08-28 12:08:52

标签: sql extract

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

时间戳消耗
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
2013-07-28 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.730
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 14 3.820
2013-07-29 01:00:32 143.820
2013-07-29 02:00:31 143.820
2013-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

[我的一条数据]

所以基本上,我需要记录每一天的记录并用第二天的值减去这些值,依此类推......


E.g。

2013-07-28 00:00:32 143,720

2013-07-29 00:00:31 143,820

2013-07-29 06:00:31 143,850

Abs(143,720 - 143,820)= 0,100

Abs(143,820 - 143,850)= 0,030



我希望输出为:

DATE CONSUMPTION

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

的用水量的示例

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分:0)

因为消费永远不会减少(小时到下一小时),你能不能

MAX(consumption) - MIN(consumption)

每个日期?

<强>更新

您可能必须这样做

MIN(consumption_today) - MIN(consumption_yesterday)

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