你的生成数据有些不对,我给矫正了!
create table t
(Log_Time datetime,Data_Value int)
insert t
select '2005-08-10 00:00:00',9 union all
select '2005-08-10 00:12:00',12 union all
select '2005-08-10 00:19:00',9 union all
select '2005-08-11 14:26:31',6 union all
select '2005-08-11 14:26:35',7 union all
select '2005-08-11 14:26:36',12 union all
select '2005-08-11 15:26:31',45 union all
select '2005-08-11 15:36:31',3 union all
select '2005-08-11 15:46:31',2 union all
select '2005-08-11 15:56:31',23 union all
select '2005-08-11 16:26:31',33 union all
select '2005-08-11 17:26:31',12 union all
select '2005-08-11 17:36:31',13 union all
select '2005-08-11 19:16:31',15 union all
select '2005-08-11 19:26:31',18 union all
select '2005-08-11 19:36:31',52
go
/*-----------创建函数来进行处理------------*/
create function f_time()
returns @t1 table(id int identity(0,1),Log_Time varchar(20),Data_Value int,Last_Data_Value int)
as
begin
declare @t table(id int identity(0,1),m datetime)
insert @t
select top 24 '00:00:00' from syscolumnsupdate @t set m=dateadd(hour,id,m)insert into @t1
select a.Log_Time,a.sum_values,b.Data_Value from 
(
select Log_Time,avg(Data_Value) as sum_values
from
(
select Log_Time=t_date+stuff(convert(varchar,m,120),1,10,''),Data_Value from
(
select * from
@t a,(
select convert(char(10),Log_Time,120) as t_date
 from t group by convert(char(10),Log_Time,120)) b
) a left join t b on convert(char(10),b.Log_Time,120)=a.t_date and a.id=datepart(hour,b.Log_Time)
) a group by Log_Time
) a left join 
(
select * from t a where 
Log_Time in (select max(Log_Time) from t group by convert(char(13),Log_Time,120))
) b on convert(char(13),a.Log_Time,120)=convert(char(13),b.Log_Time,120)
update m set Last_Data_Value=
(case when m.Last_Data_Value is null then (select top 1 Last_Data_Value from @t1
where id<m.id and Last_Data_Value is not null order by id desc) else m.Last_Data_Value end)
from @t1 m
return
end
go/*--------调用函数执行-------------*/
select Log_Time,Data_Value,Last_Data_Value from dbo.f_time()
drop function f_time
drop table t
Log_Time             Data_Value  Last_Data_Value 
-------------------- ----------- --------------- 
2005-08-10 00:00:00  10          9
2005-08-10 01:00:00  NULL        9
2005-08-10 02:00:00  NULL        9
2005-08-10 03:00:00  NULL        9
2005-08-10 04:00:00  NULL        9
2005-08-10 05:00:00  NULL        9
2005-08-10 06:00:00  NULL        9
2005-08-10 07:00:00  NULL        9
2005-08-10 08:00:00  NULL        9
2005-08-10 09:00:00  NULL        9
2005-08-10 10:00:00  NULL        9
2005-08-10 11:00:00  NULL        9
2005-08-10 12:00:00  NULL        9
2005-08-10 13:00:00  NULL        9
2005-08-10 14:00:00  NULL        9
2005-08-10 15:00:00  NULL        9
2005-08-10 16:00:00  NULL        9
2005-08-10 17:00:00  NULL        9
2005-08-10 18:00:00  NULL        9
2005-08-10 19:00:00  NULL        9
2005-08-10 20:00:00  NULL        9
2005-08-10 21:00:00  NULL        9
2005-08-10 22:00:00  NULL        9
2005-08-10 23:00:00  NULL        9
2005-08-11 00:00:00  NULL        9
2005-08-11 01:00:00  NULL        9
2005-08-11 02:00:00  NULL        9
2005-08-11 03:00:00  NULL        9
2005-08-11 04:00:00  NULL        9
2005-08-11 05:00:00  NULL        9
2005-08-11 06:00:00  NULL        9
2005-08-11 07:00:00  NULL        9
2005-08-11 08:00:00  NULL        9
2005-08-11 09:00:00  NULL        9
2005-08-11 10:00:00  NULL        9
2005-08-11 11:00:00  NULL        9
2005-08-11 12:00:00  NULL        9
2005-08-11 13:00:00  NULL        9
2005-08-11 14:00:00  8           12
2005-08-11 15:00:00  18          23
2005-08-11 16:00:00  33          33
2005-08-11 17:00:00  12          13
2005-08-11 18:00:00  NULL        13
2005-08-11 19:00:00  28          52
2005-08-11 20:00:00  NULL        52
2005-08-11 21:00:00  NULL        52
2005-08-11 22:00:00  NULL        52
2005-08-11 23:00:00  NULL        52(所影响的行数为 48 行)

解决方案 »

  1.   

    create table t
    (Log_Time datetime,Data_Value int)
    insert t
    select '2005-08-10 00:00:00',9 union all
    select '2005-08-10 00:12:00',12 union all
    select '2005-08-10 00:19:00',9 union all
    select '2005-08-11 14:26:31',6 union all
    select '2005-08-11 14:26:35',7 union all
    select '2005-08-11 14:26:36',12 union all
    select '2005-08-11 15:26:31',45 union all
    select '2005-08-11 15:36:31',3 union all
    select '2005-08-11 15:46:31',2 union all
    select '2005-08-11 15:56:31',23 union all
    select '2005-08-11 16:26:31',33 union all
    select '2005-08-11 17:26:31',12 union all
    select '2005-08-11 17:36:31',13 union all
    select '2005-08-11 19:16:31',15 union all
    select '2005-08-11 19:26:31',18 union all
    select '2005-08-11 19:36:31',52go
    declare @i int,@d datetime
    select @d=convert(varchar(10),idate,120)+' '+right(convert(varchar(13),idate,120),2)+':00' from (
    select min(log_time) as idate from t) a 
    select @i=datediff(hour,idate,adate)+1 from (select min(log_time) as idate,max(log_time) as adate from t) a
    set rowcount @i
    select sn=identity(int ,0,1) into #t from sysobjects,syscolumns,sysindexes
    set rowcount 0
    select log_hour=dateadd(hour,b.sn,@d),
           date_value=(select avg(data_value) from t where datediff(hour,dateadd(hour,b.sn,@d),log_time)=0),
           last_value=(select top 1 data_value from t where datediff(hour,dateadd(hour,b.sn,@d),log_time)<=0 order by log_time desc)
           from #t b 
    ---仅分析开始到结束时间,而不是每天,如果算每天,将开始时间和结束时间取值改一下就可以了