[firebase-br] Pra quem nunca viu uma sql deste tamanho funcionando...

Kleber Caneva kdcc em terra.com.br
Ter Maio 29 15:14:54 -03 2007


Sugiro a criação de um SP para organizar esse select...

No futuro nem mesmo você será capas de dar manutenção num select desses..

[]´s

Kléber Caneva


----- Original Message ----- 
From: "Ana Virginia da Silva" <virginia em farsoft.com.br>
To: <lista em firebase.com.br>
Sent: Saturday, May 26, 2007 11:20 AM
Subject: [firebase-br] Pra quem nunca viu uma sql deste tamanho 
funcionando...


Ai pessoal, eu ja pirei só em ter q montar esta sql...

mas funciona perfeitamente...dão um look!



Isto que é sql grande...







/*titulos quitados*/

select

case

when

(

(tf.tipo='DINHEIRO') or

((tf.tipo='CHEQUE') and

(tf.situacao='N')) or

(tf.tipo='GARANTIA') or

(tf.tipo='CORTESIA') or

(tf.tipo='CARTÃO')

) then

tf.data

else



((cast((select first(1) skip(1) tqjr.data_processamento from
titulos_quitacoes tqjr

where tqjr.is_titulofilho=tf.id_titulofilho) as date)))



end as data_pagamento,





(select es.notafiscal from estoque_saida es where es.id_estoquesaida=tp
is_movimentacao) as notafiscal,





(coalesce(tf.valor,0.00) + coalesce(tf.juros,0.00) +

coalesce(tf.multa,0.00) - coalesce(tf.desconto,0.00) -

coalesce(tf.outros_descontos,0.00) -



coalesce(coalesce((select sum(coalesce(tq.valor,0.00)) from
TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho),0.00) +

coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho

and tfx.tipo='DINHEIRO'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho

and tfx.tipo='CHEQUE' and tfx.situacao='N'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='GARANTIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and

tfx.tipo='CORTESIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='CARTÃO'),0.00) ,0
00) ) as valor_calculado,







coalesce(coalesce((select sum(coalesce(tq.valor,0.00))

from TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho),0.00) +

coalesce((select sum(coalesce(tfx.valor,0.00)) from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='DINHEIRO'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and

tfx.tipo='CHEQUE' and tfx.situacao='N'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='GARANTIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='CORTESIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='CARTÃO'),0.00) ,0
00) as valor_pago,





cop.descricao as desc_contaop, cop.conta as contaop, c.conta, c.descricao as
desc_conta,

p.nome, cf.descricao, tf.*, tp.*, tf.cpf_cnpj as "tf.cpf_cnpj",tf.numero as
tf.numero"

from TITULOS_PAI tp

left outer join TITULOS_VINCULOS tv on tv.is_titulopai=tp.id_titulopai

left outer join TITULOS_FILHOS tf on tv.is_titulofilho=tf.id_titulofilho

LEFT OUTER JOIN PESSOAS p ON tp.is_pessoa=p.id_pessoa

LEFT OUTER JOIN CONTAS c on tf.is_conta=c.id_conta

LEFT OUTER JOIN CONTAS cop ON cop.id_conta=tf.is_contaoperacao ,

CATEGORIAS_FINANCEIRO cf

where 1=1 and tp.is_categoria=cf.id_categoriafinanceira

and tp.is_empresa=1 and



(

coalesce(tf.valor,0.00) +



coalesce(tf.juros,0.00) + coalesce(tf.multa,0.00) -



coalesce(tf.desconto,0.00) - coalesce(tf.outros_descontos,0.00) -



(coalesce((select sum(tq.valor)

from TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho),0.00) +



coalesce((select sum(tfx.valor)

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho

and ( (tfx.tipo='DINHEIRO') or

((tfx.tipo='CHEQUE') and

(tfx.situacao='N')) or

(tfx.tipo='GARANTIA') or

(tfx.tipo='CORTESIA') or (tfx.tipo='CARTÃO')) ),0.00)))=0



and not ((tf.tipo='DINHEIRO') or

((tf.tipo='CHEQUE') and

(tf.situacao='N')) or

(tf.tipo='CARTÃO'))



and tf.id_titulofilho in (

select

tq.is_titulofilho from TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho)



/* and tf.id_titulofilho=247 */





and



(

case

when

(

(tf.tipo='DINHEIRO') or

((tf.tipo='CHEQUE') and

(tf.situacao='N')) or

(tf.tipo='GARANTIA') or

(tf.tipo='CORTESIA') or

(tf.tipo='CARTÃO')

) then

tf.data

else



((cast((select first(1) skip(1) tqjr.data_processamento from
titulos_quitacoes tqjr

where tqjr.is_titulofilho=tf.id_titulofilho) as date)))



end)='05/26/2007'





union



/*caixa*/

select



case

when

(

(tf.tipo='DINHEIRO') or

((tf.tipo='CHEQUE') and

(tf.situacao='N')) or

(tf.tipo='GARANTIA') or

(tf.tipo='CORTESIA') or

(tf.tipo='CARTÃO')

) then

tf.data

else

tf.vencimento

end as data_pagamento,





(select es.notafiscal from estoque_saida es where es.id_estoquesaida=tp
is_movimentacao) as notafiscal,





(coalesce(tf.valor,0.00) + coalesce(tf.juros,0.00) +

coalesce(tf.multa,0.00) - coalesce(tf.desconto,0.00) -

coalesce(tf.outros_descontos,0.00) -



coalesce(coalesce((select sum(coalesce(tq.valor,0.00)) from
TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho),0.00) +

coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho

and tfx.tipo='DINHEIRO'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho

and tfx.tipo='CHEQUE' and tfx.situacao='N'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='GARANTIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and

tfx.tipo='CORTESIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='CARTÃO'),0.00) ,0
00) ) as valor_calculado,







coalesce(coalesce((select sum(coalesce(tq.valor,0.00))

from TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho),0.00) +

coalesce((select sum(coalesce(tfx.valor,0.00)) from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='DINHEIRO'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and

tfx.tipo='CHEQUE' and tfx.situacao='N'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='GARANTIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='CORTESIA'),0.00) +



coalesce((select sum(coalesce(tfx.valor,0.00))

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho and tfx.tipo='CARTÃO'),0.00) ,0
00) as valor_pago,





cop.descricao as desc_contaop, cop.conta as contaop, c.conta, c.descricao as
desc_conta,

p.nome, cf.descricao, tf.*, tp.*, tf.cpf_cnpj as "tf.cpf_cnpj",tf.numero as
tf.numero"

from TITULOS_PAI tp

left outer join TITULOS_VINCULOS tv on tv.is_titulopai=tp.id_titulopai

left outer join TITULOS_FILHOS tf on tv.is_titulofilho=tf.id_titulofilho

LEFT OUTER JOIN PESSOAS p ON tp.is_pessoa=p.id_pessoa

LEFT OUTER JOIN CONTAS c on tf.is_conta=c.id_conta

LEFT OUTER JOIN CONTAS cop ON cop.id_conta=tf.is_contaoperacao ,

CATEGORIAS_FINANCEIRO cf

where 1=1 and tp.is_categoria=cf.id_categoriafinanceira

and tp.is_empresa=1 and



(

coalesce(tf.valor,0.00) +



coalesce(tf.juros,0.00) + coalesce(tf.multa,0.00) -



coalesce(tf.desconto,0.00) - coalesce(tf.outros_descontos,0.00) -



(coalesce((select sum(tq.valor)

from TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho),0.00) +



coalesce((select sum(tfx.valor)

from TITULOS_FILHOS tfx

where tfx.id_titulofilho=tf.id_titulofilho

and ( (tfx.tipo='DINHEIRO') or

((tfx.tipo='CHEQUE') and

(tfx.situacao='N')) or

(tfx.tipo='GARANTIA') or

(tfx.tipo='CORTESIA') or (tfx.tipo='CARTÃO')) ),0.00)))=0





and ((tf.tipo='DINHEIRO') or

((tf.tipo='CHEQUE') and

(tf.situacao='N')) or

(tf.tipo='CARTÃO'))



and not tf.id_titulofilho in (

select

tq.is_titulofilho from TITULOS_QUITACOES tq

where tq.is_titulofilho=tf.id_titulofilho)



and



(

case

when

(

(tf.tipo='DINHEIRO') or

((tf.tipo='CHEQUE') and

(tf.situacao='N')) or

(tf.tipo='GARANTIA') or

(tf.tipo='CORTESIA') or

(tf.tipo='CARTÃO')

) then

tf.data

else



(cast((select tqjr.data_processamento from titulos_quitacoes tqjr

where tqjr.is_titulofilho=tf.id_titulofilho) as date))



end)='05/26/2007'



order by 1

______________________________________________
FireBase-BR (www.firebase.com.br) - Hospedado em www.locador.com.br
Para editar sua configuração na lista, use o endereço 
http://mail.firebase.com.br/mailman/listinfo/lista_firebase.com.br
Para consultar mensagens antigas: http://firebase.com.br/pesquisa

Esta mensagem foi verificada pelo E-mail Protegido Terra.
Scan engine: McAfee VirusScan / Atualizado em 25/05/2007 / Versão: 
5.1.00/5039
Proteja o seu e-mail Terra: http://mail.terra.com.br/






Mais detalhes sobre a lista de discussão lista