Try this:
select *
from #sample a
inner join #sample b
on a.ID = b.ID and a.Date IN (b.Date - 1, b.Date - 2, b.Date - 3)
Question
Following is the sample data ,
DECLARE @maxval int, @minval int
select @maxval=201301,@minval=201312
SELECT top 100001 CAST(((@maxval + 1) - @minval) *
RAND(CHECKSUM(NEWID())) + @minval AS int)Date ,ceiling (RAND(CHECKSUM(NEWID())) *1000+25692*RAND(CHECKSUM(NEWID())) )*ceiling (RAND(CHECKSUM(NEWID())) *1000+25692*RAND(CHECKSUM(NEWID())) )ID
into #sample from sysobjects , syscolumns
Problem :
Here is the my code, It requires more manual intervention and time taking. Please assist.
select distinct date from #sample order by DATE
declare @processingdate varchar(6),@from varchar(6),@to varchar(6)
set @processingdate = '201302'
set @from = '201303'
set @to = '201305'
select distinct id into #Source from #sample where DATE = @processingdate
select distinct id into #lookup from #sample where DATE between @from and @to
select @processingdate,count(*) from #Source where ID not in (select * from #lookup)
Thanks
La solution
Try this:
select *
from #sample a
inner join #sample b
on a.ID = b.ID and a.Date IN (b.Date - 1, b.Date - 2, b.Date - 3)