有一个实时数据库,时间和流量,想构造个查询每30分钟的平均值,请问如何实现啊
2012-5-20 0:00:00 3.441
2012-5-20 0:01:00 3.504
2012-5-20 0:02:00 7.636
2012-5-20 0:03:00 8.623
2012-5-20 0:04:00 4.313
...
2012-5-20 23:36:00 3.737
2012-5-20 23:37:00 6.258
2012-5-20 23:38:00 5.039
2012-5-20 23:39:00 4.064
2012-5-20 23:40:00 5.254
查询结果
2012-5-20 0:00:00 XXX
2012-5-20 0:30:00 XXX
2012-5-20 1:00:00 XXX
2012-5-20 1:30:00 XXX
...
------解决方案--------------------
- SQL code
SELECT 日期,AVG(数值) AS 数值FROM ( SELECT DATEADD(minute,DATEDIFF(minute,0,日期)/30*30,0) AS 日期,数值 FROM 表) AS AGROUP BY 日期