表中有createtime,num两字段 
如何取每一天的各整点之间的各NUM数之和? 
假设我想取出每天8点到24点之间,各时间段内的各NUM之和。即想得到如下格式的数: 【比如2007-08-01】
8 *** 
9 *** 
10 *** 
11 *** 
12 ***
如何写SQL,谢谢!

解决方案 »

  1.   

    select convert(varchar(13),createtime,120),sum(num) 
    from tb group by convert(varchar(13)
      

  2.   


    select sum(num)
    from tb
    where datepart(hh,createtime) between 8 and 24
    group by convert(varchar(8),createtime,112)
      

  3.   

    select convert(varchar(13),createtime,120),sum(num)
    from tb
    group by convert(varchar(13),createtime,120)
      

  4.   


    select datepart(hh,createtime) [hour],sum(num) num
    from tb
    where datepart(hh,createtime) between 8 and 24
    group by convert(varchar(13),createtime,120)
      

  5.   

    select convert(varchar(13),createtime,120),sum(num)
    from tb
    where convert(varchar(10),createtime,120)='2007-08-01' and datepart(hh,creattime) between 8 and 23
    group by convert(varchar(13),createtime,120)
      

  6.   

    谢谢大家,只是刚才少问了一句,就是求平均值。 比如举例 8点只有3条数据的话
    时间     NUM 
    8:01   100
    8:02   101
    8:03   102就想得到 8点的这个平均值     (100+101+102)/3
    同样得到9点的平均值、10点的平均值.............
      

  7.   

    select datepart(hh,creattime),avg(num) as 平均值
    from tb
    where convert(varchar(10),createtime,120)='2007-08-01' and datepart(hh,creattime) between 8 and 23
    group by datepart(hh,creattime)
      

  8.   

    求平均值:
    select convert(varchar(13),createtime,120),avg(num) 
    from tb group by convert(varchar(13)
      

  9.   


    用AVG()这样解决的。谢谢
      

  10.   

    谢谢qianjin036a(晴天), AcHerat(园丁小三 努力结贴), FlySQL(FlySQL)。