[firebase-br] Union x order by
    Ana Virginia da Silva 
    virginia em farsoft.com.br
       
    Sáb Maio 26 17:24:35 -03 2007
    
    
  
Pessoal estou fazendo a union abaixo, porém, 
ela só me deixa jogar o order by para um campo...isto é normal...eu não 
posso ordenar por outros campos??? 
 
 
/*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(0) tfjr.data from titulos_quitacoes tqjr, 
titulos_vinculos tvjr, titulos_pai tpjr, titulos_filhos tfjr 
where tqjr.is_titulofilho=tf.id_titulofilho 
and tvjr.is_titulopai=tqjr.is_titulopai 
and tvjr.is_titulofilho=tfjr.id_titulofilho 
and tpjr.id_titulopai=tqjr.is_titulopai 
) 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(0) tfjr.data from titulos_quitacoes tqjr, 
titulos_vinculos tvjr, titulos_pai tpjr, titulos_filhos tfjr 
where tqjr.is_titulofilho=tf.id_titulofilho 
and tvjr.is_titulopai=tqjr.is_titulopai 
and tvjr.is_titulofilho=tfjr.id_titulofilho 
and tpjr.id_titulopai=tqjr.is_titulopai 
) as date))) 
 
end)>='5/01/2007' 
 
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(0) tfjr.data from titulos_quitacoes tqjr, 
titulos_vinculos tvjr, titulos_pai tpjr, titulos_filhos tfjr 
where tqjr.is_titulofilho=tf.id_titulofilho 
and tvjr.is_titulopai=tqjr.is_titulopai 
and tvjr.is_titulofilho=tfjr.id_titulofilho 
and tpjr.id_titulopai=tqjr.is_titulopai 
) as date))) 
 
end)<='5/31/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/1/2007' 
 
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/31/2007' 
 
order by 1
    
    
Mais detalhes sobre a lista de discussão lista