如题。有一个时间,和数值举例如下createtime num
2012-04-07 1:01 1
2012-04-07 1:12 20
2012-04-07 3:01 100
2012-04-07 4:12 201
.........................................怎么求Num前10小时数据的平均值。谢谢,在线等。
2012-04-07 1:01 1
2012-04-07 1:12 20
2012-04-07 3:01 100
2012-04-07 4:12 201
.........................................怎么求Num前10小时数据的平均值。谢谢,在线等。
select avg(num) from tablea where createtime>=DATEADD(hh,-10,GetDate()) and createtime<= GetDate()
(
createtime DATETIME,
num int )
INSERT INTO @xxx
SELECT '2012-04-07 1:01', 1 UNION ALL
SELECT '2012-04-07 1:12', 20 UNION ALL
SELECT '2012-04-07 13:01', 100 UNION ALL
SELECT '2012-04-07 14:12', 202 --SELECT * FROM @xxxselect avg(num) AS AvgNum from @xxx where createtime>=DATEADD(hh,-10,GetDate()) and createtime<= GetDate()
/*(所影响的行数为 4 行)AvgNum
-----------
151(所影响的行数为 1 行)
*/