22,209
社区成员
发帖
与我相关
我的任务
分享
create table tb(cid varchar(10),cdate datetime,cnum int)
insert into tb values('0001', '2011-04-12', 12)
insert into tb values('0001', '2011-04-11', 11)
insert into tb values('0002', '2011-04-12', 20)
insert into tb values('0002', '2011-04-11', 18)
insert into tb values('0003', '2011-04-10', 30)
go
declare @sdate datetime
declare @edate datetime
set @sdate = '2011-04-11'
set @edate = '2011-04-13'
select p.cid , p.dt cdate , cnum = (case when p.dt = q.cdate and p.cid = q.cid then q.cnum
else (select top 1 cnum from tb o where p.cid = o.cid order by o.cdate desc) end) from
(
select m.* , n.cid from
(
select
dateadd(dd,num,@sdate) dt
from
(select isnull((select count(1) from sysobjects where id<t.id),0) as num from sysobjects t) a
where
dateadd(dd,num,@sdate)<=@edate
) m,
(
select distinct cid from tb
) n
) p left join tb q
on p.dt = q.cdate and p.cid = q.cid
order by p.cid , cdate desc
drop table tb
/*
cid cdate cnum
---------- ------------------------------------------------------ -----------
0001 2011-04-13 00:00:00.000 12
0001 2011-04-12 00:00:00.000 12
0001 2011-04-11 00:00:00.000 11
0002 2011-04-13 00:00:00.000 20
0002 2011-04-12 00:00:00.000 20
0002 2011-04-11 00:00:00.000 18
0003 2011-04-13 00:00:00.000 30
0003 2011-04-12 00:00:00.000 30
0003 2011-04-11 00:00:00.000 30
(所影响的行数为 9 行)
*/
create table tb(cid varchar(10),cdate datetime,cnum int)
insert into tb
select '0001' ,'2011-04-12' ,12 union all
select '0001' ,'2011-04-11' ,11 union all
select '0002' ,'2011-04-12' ,20 union all
select '0002' ,'2011-04-11' ,18 union all
select '0003' ,'2011-04-10' ,30
go
declare @start datetime
declare @end datetime
set @start = '2011-04-11'
set @end = '2011-04-13'
select distinct a.cid,dateadd(dd,b.number,@start) as date
into #tb
from tb a,master..spt_values b
where b.[type] = 'p' and b.number between 0 and datediff(dd,@start,@end)
select a.cid,a.date,(case when b.cnum is null then (select cnum from tb
where cdate = (select max(cdate) from tb where cid = a.cid) and cid = a.cid)
else b.cnum end) cnum
from #tb a left join tb b on a.cid = b.cid and a.date = b.cdate
order by a.cid,a.date
drop table tb,#tb
/*
cid date cnum
---------- ----------------------- -----------
0001 2011-04-11 00:00:00.000 11
0001 2011-04-12 00:00:00.000 12
0001 2011-04-13 00:00:00.000 12
0002 2011-04-11 00:00:00.000 18
0002 2011-04-12 00:00:00.000 20
0002 2011-04-13 00:00:00.000 20
0003 2011-04-11 00:00:00.000 30
0003 2011-04-12 00:00:00.000 30
0003 2011-04-13 00:00:00.000 30
(9 行受影响)
create function generateTimeV2
(
@begin_date datetime,
@end_date datetime
)
returns @t table(date datetime)
as
begin
insert into @t
select dateadd(dd,number,@begin_date) AS date
from master..spt_values
where type='p' and dateadd(dd,number,@begin_date)<=@end_date
return
END
declare @t table (cid varchar(4),cdate datetime,cnum int)
insert into @t
select '0001','2011-04-12',12 union all
select '0001','2011-04-11',11 union all
select '0002','2011-04-12',20 union all
select '0002','2011-04-11',18 union all
select '0003','2011-04-10',30
SELECT aa.date,aa.cid,bb.cnum FROM (
select
convert( varchar(10),date,120) AS date,b.cid from dbo.generateTimeV2('2011-04-11','2011-04-13')
CROSS JOIN (select distinct cid from @t) b
) aa LEFT JOIN @t bb
ON aa.date=bb.cdate AND aa.cid=bb.cid
/*
date cid cnum
---------- ---- -----------
2011-04-11 0001 11
2011-04-11 0002 18
2011-04-11 0003 NULL
2011-04-12 0001 12
2011-04-12 0002 20
2011-04-12 0003 NULL
2011-04-13 0001 NULL
2011-04-13 0002 NULL
2011-04-13 0003 NULL
*/