日期:2014-05-18 浏览次数:20753 次
select convert(varchar(10),createtime,120) date,datepart(hh,createtime) hh,sum(xiaoshoushu) xiaoshu from tb where convert(varchar(10),createtime,120) = '2012-03-09' group by convert(varchar(10),createtime,120),datepart(hh,createtime)
------解决方案--------------------
如果中间有缺省的也要统计进去,楼主可以自己设计一个对应时间段的临时表去left join主表,继而进行相应的统计。
------解决方案--------------------
create table tb(id int,createtime datetime,xiaoshoushu int) insert into tb select 1,'2012-03-09 01:01:00.000',1 insert into tb select 2,'2012-03-09 01:02:00.000',2 insert into tb select 3,'2012-03-09 02:15:00.000',3 insert into tb select 4,'2012-03-09 03:01:00.000',5 insert into tb select 5,'2012-03-09 03:15:00.000',1 insert into tb select 6,'2012-03-09 05:15:00.000',3 insert into tb select 7,'2012-03-09 05:35:00.000',4 insert into tb select 8,'2012-03-09 05:45:00.000',3 go select convert(varchar(13),createtime,120),sum(xiaoshoushu) from tb group by convert(varchar(13),createtime,120) /* ------------- ----------- 2012-03-09 01 3 2012-03-09 02 3 2012-03-09 03 6 2012-03-09 05 10 (4 行受影响) */ go drop table tb