【发布时间】:2018-03-13 17:59:32
【问题描述】:
我有一个像这样的简单表格(以实际 x 千值表示):
在这张表上,我正在做一个查询以获取差异(效果很好):
select
EL.sensor_id,
EL.value_id,
EL.time,
EL.value,
if( @lastSN = EL.sensor_id, EL.value - @lastValue, 0000.00 ) as diff,
@lastSN := EL.sensor_id,
@lastValue := EL.value
from
data_test_debug EL,
( select @lastSN := 0,
@lastValue := 0 ) SQLVars
where EL.value_id = "gas"
order by
EL.sensor_id,
EL.time
结果如下:
现在我遇到了一个问题,因为我必须对差异数据进行分组。
按天、月、周或年(用于在图表中表示数据)
我尝试过这样的事情:
select MONTHNAME(EL.time), SUM(EL.diff)
FROM data_test_debug EL,
(select
EL.sensor_id,
EL.value_id,
EL.time,
EL.value,
if( @lastSN = EL.sensor_id, EL.value - @lastValue, 0000.00 ) as diff,
@lastSN := EL.sensor_id,
@lastValue := EL.value
from
data_test_debug EL,
( select @lastSN := 0,
@lastValue := 0 ) SQLVars
where EL.value_id = "gas"
order by
EL.sensor_id,
EL.time) AS t
GROUP BY YEAR(EL.time), MONTH(EL.time)
但没有期待得到结果..
数据每小时收集一次,因此需要分组。
【问题讨论】: