![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
--
这个按每五分钟的,你稍微改改就行了. [code=SQL]create table tb(时间 datetime , 金额 int) insert into tb values('2007-1-1 10:00:23' , 8 ) insert into tb values('2007-1-1 10:01:24' , 4 ) insert into tb values('2007-1-1 10:05:00' , 2 ) insert into tb values('2007-1-1 10:06:12' , 3 ) insert into tb values('2007-1-1 10:08:00' , 1 ) insert into tb values('2007-1-1 10:12:11' , 5 ) go --时间段>=10:00:00 and 时间段 <10:05:00 select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段, count(*) as 行数, sum(金额) as 总金额 from tb group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120)) /* 时间段 行数 总金额 ------------------------------------------------------ ----------- ----------- 2007-01-01 10:00:00.000 3 14 2007-01-01 10:05:00.000 2 4 2007-01-01 10:10:00.000 1 5 (所影响的行数为 3 行) */ --时间段>10:00:00 and 时间段 <=10:05:00 select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段, count(*) as 行数, sum(金额) as 总金额 from tb group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120)) /* 时间段 行数 总金额 ------------------------------------------------------ ----------- ----------- 2007-01-01 10:00:00.000 2 12 2007-01-01 10:05:00.000 3 6 2007-01-01 10:10:00.000 1 5 (所影响的行数为 3 行) */ drop table tb