[firebase-br] Pra quem nunca viu uma sql deste tamanho funcionando...
Ana Virginia da Silva
virginia em farsoft.com.br
Sáb Maio 26 11:20:24 -03 2007
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
Mais detalhes sobre a lista de discussão lista