如题。有一个时间,和数值举例如下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小时数据的平均值。谢谢,在线等。     

解决方案 »

  1.   

    select avg(num) from tablea where createtime>='2012-04-07 00:00'createtime<='2012-04-07 10:00'
      

  2.   


    select avg(num) from tablea where createtime>=DATEADD(hh,-10,GetDate()) and createtime<= GetDate()
      

  3.   

    select avg(num) from tablea where createtime>=dateadd(hh,-10,getdate())
      

  4.   

    Test DataDECLARE @xxx TABLE
    (
      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 行)
    */