sqlserver 按五分钟分组
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
最简单方法,把datetime转成float型(单位整数部分为天),然后乘24*60/5,就是整数部分是5分钟了,然后取整就行了
用这种方法做,随便你算几分钟的分组都能算
select cast(floor(cast(时间 as float)*24*60/5)*5/60/24 as smalldatetime),SUM(金额)
from tb
group by cast(floor(cast(时间 as float)*24*60/5)*5/60/24 as smalldatetime)