0

我正在尝试按小时进行分组查询,同时对每小时的数据值进行平均。每分钟有一个数据值:

1) 为什么当数据库中有更多行时我只返回 24 行?

2) 为什么我没有得到最新的记录?ValueID 是我的 PK,它会为每条新记录自动递增,并在查询中对其进行排序。

3)有什么办法可以加快速度吗?

这是我的数据的样子:

    AVG(DataValue)      LocalDateTime   ValueID
4.62626388888888    2013-10-17 03:00:00 15220669
4.628513888888879   2013-10-17 02:00:00 15220609
4.6264722222222066  2013-10-17 01:00:00 15220549
4.626902777777761   2013-10-17 00:00:00 15220489
4.625777777777772   2013-10-16 23:00:00 15220429
4.6249444444444325  2013-10-16 22:00:00 15220369
4.626000000000004   2013-10-16 21:00:00 15220309
4.626999999999997   2013-10-16 20:00:00 15220249
4.626597222222213   2013-10-16 19:00:00 15220189
4.624853963838648   2013-10-16 18:00:00 15220129
4.627216783216766   2013-10-16 17:00:00 15220069
4.625388888888868   2013-10-16 16:00:00 15220009
4.622531293463151   2013-10-16 15:00:00 15219950
4.626722222222235   2013-10-16 14:00:00 15219890
4.6241249999999985  2013-10-16 13:00:00 15219830
4.626390449438195   2013-10-16 12:00:00 15219770
4.6337862796833695  2013-10-16 11:00:00 15219710
4.6246410256410275  2013-10-16 10:00:00 15219650
4.628753351206421   2013-10-16 09:00:00 15219590
4.628453865336649   2013-10-16 08:00:00 15219530
4.628678571428564   2013-10-16 07:00:00 15219470
4.626761904761896   2013-10-16 06:00:00 15219410
4.631481942714808   2013-10-16 05:00:00 9629712
4.626550387596896   2013-10-16 04:06:00 9629658

这是查询:

SELECT * FROM 
  (SELECT 
    AVG(DataValue),
    LocalDateTime, 
    ValueID 
  FROM
    odm.datavalues
  WHERE
    SiteID = 10 and VariableID = 8
    and OffsetValue = -7
  GROUP BY HOUR(LocalDateTime))
AS tmp  ORDER BY ValueID DESC LIMIT 2000 

如果有帮助,这里是一些没有分组的示例数据:

AVG(DataValue) LocalDateTime ValueID
4.67    2013-10-29 08:40:00 24057189
4.67    2013-10-29 08:39:00 24057188
4.67    2013-10-29 08:38:00 24057187
4.67    2013-10-29 08:37:00 24057186
4.67    2013-10-29 08:36:00 24057185
4.66    2013-10-29 08:35:00 24057184
4.67    2013-10-29 08:34:00 24057183
4.67    2013-10-29 08:33:00 24057182
4.67    2013-10-29 08:32:00 24057181
4.67    2013-10-29 08:31:00 24057180
4.67    2013-10-29 08:30:00 24057179
4.67    2013-10-29 08:29:00 24057178
4.67    2013-10-29 08:28:00 24057177
4.67    2013-10-29 08:27:00 24057176
4.67    2013-10-29 08:26:00 24057175
4.67    2013-10-29 08:25:00 24057174
4.67    2013-10-29 08:24:00 24057173
4.67    2013-10-29 08:23:00 24057172
4.67    2013-10-29 08:22:00 24057171
4.67    2013-10-29 08:21:00 24057170
4.67    2013-10-29 08:20:00 24057169
4.67    2013-10-29 08:19:00 24057168
4.67    2013-10-29 08:18:00 24057167
4.67    2013-10-29 08:17:00 24057166

谢谢你的帮助!

4

0 回答 0