declare @t datetime
declare @m datetime
select * into #t from r where 1 = 2
select @t = min(dt) from r
select @m = max(dt) from r
while @t <= @m
begin
if exists (select * from r where dt = @t)
insert #t select id, dt, count from r where dt = @t
else
insert #t select 0, @t, 0
@t = dateadd(day, 1, @t)
end
select * from #t order by dt
go
1,建序数表
select top 8000 identity(int,0,1) as N into numtab from
(select top 100 id=1 from sysobjects) as a,
(select top 100 id=1 from sysobjects) as b,
(select top 100 id=1 from sysobjects) as c
2,
select cast('2003-4-1' as datetime)+a.n ,isnull(b.count,0)
from numtab a left join r b on convert(char(10),cast('2003-4-1' as datetime)+a.n,120)=convert(char(10),b.dt,120)
where cast('2003-4-1' as datetime)+a.n<='2003-06-01'
1,建序数表
select top 8000 identity(int,0,1) as N into numtab from
(select top 100 id=1 from sysobjects) as a,
(select top 100 id=1 from sysobjects) as b,
(select top 100 id=1 from sysobjects) as c
2,
select cast('2003-4-1' as datetime)+a.n
from numtab a left join r b on convert(char(10),cast('2003-4-1' as datetime)+a.n,120)=convert(char(10),b.dt,120)
where cast('2003-4-1' as datetime)+a.n<='2003-06-01'