Ajuda com essa SQL

Firebird

26/03/2007

Bom dia

Estou com esse problema...
tenho uma tabela (ControleReceber) onde eu gravo 3 campos com códigos (Cliente, Representante, Funcionario) até aí td bem. Só que eu preciso puxar a cidade (CadCidade) e o bairro de cada um e trazer na tela... Portanto são 5 tabelas diferentes numa mesma SQL. tentei fazer do modo tradicional, comparando os campos, e com Left Join, e estava demorando muito (cerca de 10 minutos e contando), tem algum jeito mais facil para isso?


Facc

Facc

Curtidas 0

Respostas

Gandalf.nho

Gandalf.nho

26/03/2007

Exatamente como você montou sua SQL? Existe relacionamentos entre as tabelas envolvidas?


GOSTEI 0
Facc

Facc

26/03/2007

Exatamente como você montou sua SQL? Existe relacionamentos entre as tabelas envolvidas?


Através do campo código

aí que tá... nem sei por onde começar[/code]


GOSTEI 0
Facc

Facc

26/03/2007

Originalmente era assim a SQL
SELECT a.*, b.Descr_Cid, c.Bair_Cli FROM CONTROLERECEBER a, CadCidade b, CadCliComercio c 
WHERE ((NTROCA IS NOT NULL) or (NTROCA <> ""))
AND a.Cliente = c.Cod_Cli and c.Cid_Cli = b.Cod_Cid



Mas surgiu a necessidade de acrescentar o Representante e Funcionario, e aí que surgiu a dúvida


GOSTEI 0
Facc

Facc

26/03/2007

tentei dessa forma...

select a.*,
       case a.tiporec
         when "C" then a.cliente
         when "R" then a.cod_repres
         when "O" then a.codoficina
         when "F" then a.codfunc
         when "N" then a.cod_for
       end CodDevedor,
       case a.tiporec
         when "C" then b.bair_cli
         when "R" then c.bair_vend
         when "O" then d.bair_ofi
         when "F" then e.bairro_func
         when "N" then f.bair_for
       end Bairro,
       g.descr_cid
from controlereceber a, cadcidade g
left join cadclicomercio b on
  b.cod_cli = a.cliente and
  b.cid_cli = g.cod_cid
left join cadvendedor c on
  c.cod_vend = a.cod_repres and
  c.cid_vend = g.cod_cid
left join cadoficina d on
  d.cod_ofi = a.codoficina and
  d.cid_ofi = g.cod_cid
left join cadfuncionario e on
  e.codigo_func = a.codfunc and
  e.cod_cidade = g.cod_cid
left join cadfornecedor f on
  f.cod_for = a.cod_for and
  f.cid_for = g.cod_cid


mas dá erro


GOSTEI 0
Facc

Facc

26/03/2007

Consegui!!!

select a.*,
       case a.tiporec
         when "C" then a.cliente
         when "R" then a.cod_repres
         when "O" then a.codoficina
         when "F" then a.codfunc
         when "N" then a.cod_for
       end CodDevedor,
       case a.tiporec
         when "C" then b.bair_cli
         when "R" then c.bair_vend
         when "O" then d.bair_ofi
         when "F" then e.bairro_func
         when "N" then f.bair_for
       end Bairro,
       case a.tiporec
         when "C" then b.cid_cli
         when "R" then c.cid_vend
         when "O" then d.cid_ofi
         when "F" then e.cod_cidade
         when "N" then f.cid_for
       end CodCidade
from controlereceber a
left join cadclicomercio b on
  b.cod_cli = a.cliente
left join cadvendedor c on
  c.cod_vend = a.cod_repres
left join cadoficina d on
  d.cod_ofi = a.codoficina
left join cadfuncionario e on
  e.codigo_func = a.codfunc
left join cadfornecedor f on
  f.cod_for = a.cod_for



GOSTEI 0
POSTAR