Tenho uma tabela com numeros sequenciais e preciso saber se houve quebra nessa sequencia e quais numeros estao faltando, tipo:
1
2
3
5
6
7
Que Query poderia mim retonar informando que o numero 4 esta faltando na sequencia?
Moderador: Moderadores
1
2
3
5
6
7
select a.id -1 from tabela a.id <> 1 and not exists (select id from tabela b where b.id = (a.id-1) )
create table a03ped ( NumCCF int not null primary key );
insert into a03ped (NumCCF) values (1), (2), (3), (5), (6), (7);
select start, stop from (
select m.NumCCF + 1 as start,
(select min(NumCCF) - 1 from a03ped as x where x.NumCCF > m.NumCCF) as stop
from a03ped as m
left outer join a03ped as r on m.NumCCF = r.NumCCF - 1
where r.NumCCF is null
) as x
where stop is not null;
stat func FverPulado(xtabela,xcampo)
/*
exemplo de chamada
nomeMatrizRetorno:=FverPulado( "cadcli","codigo")
verifica se tem numeracao pulado na tabela
retorna matriz com os numeros pulados
fb_executa é a minha rotina para executar o str, veja a sua
m_select ´e oretorno da minha funcao f_executa()
uso strzero e val , porque os meus codigo sao sempre completos , exe 000001
*/
local xmin,xmax , m_pulados:={} , a
Str:="select Min("+xcampo+"),Max( "+xcampo+") from "+xtabela+";" // vefifico a faixa de pesquisa
fb_executa() // -é a minha rotina que executa o str e retorna matriz m_select, verifique a sua
xmin:=m_select[1,1]
xmax:= m_select[1,2]
for a:= val(xmin) to val(xmax)
// strzero, porque só trablho com numeros completos do tipo "00001"
str:="select first 1 "+xcampo+" from "+xtabela + " where "+xcampo+" = '"+ strzero(a,len(xmin)) +"';"
fb_executa()
if empty( m_select)
aadd(m_pulados,strzero(a,len(xmin) ) )
endif
next
msginfo( str(len(m_pulados),5) +" registros pulados")
retu m_pulados
Usuários vendo este fórum: Nenhum usuário registrado online e 8 visitantes