日期:2014-05-17  浏览次数:20535 次

关于时间段的求和
求一天数据和,如下
icd                    tm                                     x
1300     2012-06-04 22:00:00.000 57.84                 50
1300     2012-06-04 22:05:00.000 57.84                 50
1300     2012-06-04 22:10:00.000 57.84                 50                  
1300     2012-06-04 22:15:00.000 57.84                 50
1311     2012-06-04 22:20:00.000 57.84                 50
1311     2012-06-04 22:25:00.000 57.84                 50
1311     2012-06-04 22:30:00.000 57.84                 50
1311     2012-06-04 22:35:00.000 57.84                 50


输出
icd              tm_day                                     sum_x
1300     2012-06-04                                  200
1311      2012-06-04                                  200

谢谢!

------解决方案--------------------
create table tb(icd int,tm datetime,y decimal(10,2),x int)
insert into tb select 1300,'2012-06-04 22:00:00.000',57.84,50
insert into tb select 1300,'2012-06-04 22:05:00.000',57.84,50
insert into tb select 1300,'2012-06-04 22:10:00.000',57.84,50                  
insert into tb select 1300,'2012-06-04 22:15:00.000',57.84,50
insert into tb select 1311,'2012-06-04 22:20:00.000',57.84,50
insert into tb select 1311,'2012-06-04 22:25:00.000',57.84,50
insert into tb select 1311,'2012-06-04 22:30:00.000',57.84,50
insert into tb select 1311,'2012-06-04 22:35:00.000',57.84,50
go
select icd,convert(varchar(10),tm,120) as tm_day,sum(x) sum_x
from tb
group by icd,convert(varchar(10),tm,120)
/*
icd      &nb