Inquiridor
Não quero repetir os parametros ...

Pergunta
-
Pessoal, tenho a query abaixo, no fim tenho um or que repete alguns parametros já utilizados. preciso ajustar a query para que faça tudo isso ou tudo isso mais ligacoes com o tamanho de 2 caracteres Len(nrdiscado) = 2.
Existe uma forma melhor?
Declare @central as int
Declare @dt_inicio as smalldatetime
Declare @dt_fim as smalldatetimeSet @central = 21
Set @dt_inicio = '2007-01-01'
Set @dt_fim = '2007-01-31'
select * from ligacoes_particoes where right(nrdiscado,4)
in
(select right(faixa,4) from voip_numeros where pkcdcentral in (17,19) and right(faixa,4)<>'9710')
and pkcdcentral = @central and dtligacao Between @dt_inicio and @dt_fim
and duracao > 10 and duracao < 14400
and (numerotronco between '710001' and '710060' and PKcdFluxo = 'SD')
-- '710001' and '710060' UIB
-- '3001' and '3060' PE_PP
-- '3001' and '3030' CS_PVC
-- '200001' and '200030' PVC_CS
--and substring (nrdiscado,1,2) <> '71' and substring (nrdiscado,1,3) <> '071'and substring (nrdiscado,1,2) <> '11' and substring (nrdiscado,1,3) <> '011'
and substring (nrdiscado,1,2) <> '51' and substring (nrdiscado,1,3) <> '011'
Daqui pra frente -->>
or dtligacao between '2007-01-01' and '2007-01-31' and pkcdcentral = 21 and numerotronco between '710001' and '710060' and PKcdFluxo = 'SD' and len(nrdiscado) = 2
Obrigado !
Todas as Respostas
-
Boa tarde Paulo.
A solução é simples basta vc atribuir os valores para variáveis segue um exemplo abaixo:
Declare @central as int
Declare @dt_inicio as smalldatetime
Declare @dt_fim as smalldatetimeDeclare @Trocoini varchar(10),
@troncofim varchar(10)
Set @Troncoini = '710001'
Set @Troncofim = '710060'
Set @central = 21
Set @dt_inicio = '2007-01-01'
Set @dt_fim = '2007-01-31'
select * from ligacoes_particoes where right(nrdiscado,4)
in
(select right(faixa,4) from voip_numeros where pkcdcentral in (17,19) and right(faixa,4)<>'9710')
and pkcdcentral = @central and dtligacao Between @dt_inicio and @dt_fim
and duracao > 10 and duracao < 14400
and (numerotronco between '710001' and '710060' and PKcdFluxo = 'SD')
-- '710001' and '710060' UIB
-- '3001' and '3060' PE_PP
-- '3001' and '3030' CS_PVC
-- '200001' and '200030' PVC_CS
--and substring (nrdiscado,1,2) <> '71' and substring (nrdiscado,1,3) <> '071'and substring (nrdiscado,1,2) <> '11' and substring (nrdiscado,1,3) <> '011'
and substring (nrdiscado,1,2) <> '51' and substring (nrdiscado,1,3) <> '011'
Daqui pra frente -->>
or dtligacao between @Dt_Inicio and @Dt_Fim and pkcdcentral = 21 and numerotronco between @TroncoIni and @TroncoFim and PKcdFluxo = 'SD' and len(nrdiscado) = 2
Seria isso ?
-
Primeiro de tudo, cuidado ao utilizar OR com vários AND's cada um. Quando for fazer isso, coloque dentro de parânteses, como no exemplo abaixo, pois o seu OR não iria funcionar corretamente. A consulta iria retornar APENAS os números cujo tamanho seja igual a 2!
(
TESTE 1
AND TESTE 2
)
OR
(
TESTE 3
AND TESTE 4
)
Seria algo como isso que vc está procurando?
DECLARE @CENTRAL AS INT
DECLARE @DT_INICIO AS SMALLDATETIME
DECLARE @DT_FIM AS SMALLDATETIME
SET @CENTRAL = 21
SET @DT_INICIO = '2007-01-01'
SET @DT_FIM = '2007-01-31'
SELECT *
FROM LIGACOES_PARTICOES
WHERE PKCDCENTRAL = @CENTRAL
AND DTLIGACAO BETWEEN @DT_INICIO AND @DT_FIM
AND NUMEROTRONCO BETWEEN '710001' AND '710060'
AND PKCDFLUXO = 'SD'
AND (
(
LEN(NRDISCADO) = 2
)
OR
(
RIGHT(NRDISCADO,4) IN (SELECT RIGHT(FAIXA,4)
FROM VOIP_NUMEROS
WHERE PKCDCENTRAL IN (17,19)
AND RIGHT(FAIXA,4) <> '9710')
AND DURACAO > 10
AND DURACAO < 14400
AND SUBSTRING (NRDISCADO,1,2) <> '11' AND SUBSTRING (NRDISCADO,1,3) <> '011'
AND SUBSTRING (NRDISCADO,1,2) <> '51' AND SUBSTRING (NRDISCADO,1,3) <> '011'
)
)
Espero ter ajudado!