我有一个表 
 编号        时间                    方量
001        2010-01-01 08:01:01        7
002        2010-01-01 08:20:01        5
003        2010-01-01 09:11:56        12
004        2010-01-01 10:20:01        5
005        2010-01-01 11:20:01        5用什么语句得到以下结果8点       129点       1210点      5
11点      5就是按时间段统计

解决方案 »

  1.   

    create table tb(编号 varchar(10),时间 datetime,方量 int)
    insert into tb select '001','2010-01-01 08:01:01',7
    insert into tb select '002','2010-01-01 08:20:01',5
    insert into tb select '003','2010-01-01 09:11:56',12
    insert into tb select '004','2010-01-01 10:20:01',5
    insert into tb select '005','2010-01-01 11:20:01',5
    go
    select datepart(hh,时间),sum(方量) from tb group by datepart(hh,时间)
    go
    drop table tb
    /*
    ----------- -----------
    8           12
    9           12
    10          5
    11          5(4 行受影响)*/
      

  2.   

    create table tb(编号 varchar(10),时间 datetime,方量 int)
    insert into tb select '001','2010-01-01 08:01:01',7
    insert into tb select '002','2010-01-01 08:20:01',5
    insert into tb select '003','2010-01-01 09:11:56',12
    insert into tb select '004','2010-01-01 10:20:01',5
    insert into tb select '005','2010-01-01 11:20:01',5
    go
    select convert(varchar,datepart(hh,时间))+'点',sum(方量) from tb group by datepart(hh,时间)
    go
    drop table tb
    /*
    ----------- -----------
    8点                               12
    9点                               12
    10点                              5
    11点                              5
    (4 行受影响)*/
      

  3.   

    select HOUR(时间),sum(方量) from T1 group by HOUR(时间)