Pergunta

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 :

  • there are 2 columns date & id
  • take first month id's , say for example 201302
  • need to find whether those id's are available or not, in the next 3 months. so here 201303,201304,201305
  • need to repeat this for all months

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

Foi útil?

Solução

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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top