--給你看下結果
create table t (code int,tm datetime,value int)
insert into t
select 1,'2006-09-16 00:00:10',10 union all
select 1,'2006-09-16 00:00:30',20 union all
select 1,'2006-09-16 00:00:50',30 union all
select 1,'2006-09-16 00:01:10',10 union all
select 1,'2006-09-16 00:01:30',20 union all
select 1,'2006-09-16 00:01:50',30 union all
select 1,'2006-09-16 00:02:10',10 union all
select 1,'2006-09-16 00:02:30',20 union all
select 1,'2006-09-16 00:02:50',30 union all
select 1,'2006-09-16 00:03:10',10 union all
select 1,'2006-09-16 00:03:30',20 union all
select 1,'2006-09-16 00:03:50',30 union all
select 1,'2006-09-16 00:04:10',10 union all
select 1,'2006-09-16 00:04:30',20 union all
select 1,'2006-09-16 00:04:50',30 union all
select 1,'2006-09-16 00:05:10',1 union all
select 1,'2006-09-16 00:05:30',2 union all
select 1,'2006-09-16 00:05:50',3 union all
select 1,'2006-09-16 00:06:10',1 union all
select 1,'2006-09-16 00:06:30',2 union all
select 1,'2006-09-16 00:06:50',3 union all
select 1,'2006-09-16 00:07:10',1 union all
select 1,'2006-09-16 00:07:30',2 union all
select 1,'2006-09-16 00:07:50',3 union all
select 1,'2006-09-16 00:08:10',1 union all
select 1,'2006-09-16 00:08:30',2 union all
select 1,'2006-09-16 00:08:50',3 union all
select 1,'2006-09-16 00:09:10',1 union all
select 1,'2006-09-16 00:09:30',2 union all
select 1,'2006-09-16 00:09:50',3
union all
select 2,'2006-09-16 01:09:50',100
-------------------------------------------------
declare @tm datetime
set @tm=(select min(tm) from t)
select * into #t from
(select code,datediff(minute ,@tm,tm)/5 as min_time, avg(value) as value
from T
group by code,datediff(minute ,@tm,tm)/5) A
select code,convert(varchar(16),dateadd(minute,min_time*5,@tm),120)+'到'+convert(varchar(16),dateadd(minute,(min_time+1)*5-1,@tm),120) as time_range,value from #t