2
I am running the following select on Oracle SQL Developer:
SELECT SUM(FANFISCA.TOTAL_NF) AS TOTAL_FATURAMENTO,
FANFISCA.CD_REPRESENTANT COD_REPRESENTANTE,
GEEMPRES.NOME_COMPLETO REPRESENTANTE
FROM FANFISCA,GEEMPRES
WHERE GEEMPRES.CD_EMPRESA = FANFISCA.CD_REPRESENTANT
GROUP BY FANFISCA.CD_REPRESENTANT, GEEMPRES.NOME_COMPLETO
UNION ALL
SELECT SUM(FAPEDIDO.TOTAL_FATURAMENTO) AS TOTAL_PEDIDO,
FAPEDIDO.CD_REPRESENTANT COD_REPRESENTANTE,
GEEMPRES.NOME_COMPLETO REPRESENTANTE
FROM FAPEDIDO,GEEMPRES
WHERE FAPEDIDO.CD_REPRESENTANT = GEEMPRES.CD_EMPRESA
AND FAPEDIDO.CONTROLE BETWEEN '05' AND '15'
GROUP BY GEEMPRES.NOME_COMPLETO,FAPEDIDO.CD_REPRESENTANT
Below is the result of the consultation:
I need to return TOTAL_FATURAMENTO and TOTAL_PEDIDO in 2 different columns, as you can see in the image, the query returns all in the same column of TOTAL_FATURAMENTO.
This worked very well, but there is an ambiguity error when we leave COD_REPRESENTANTE, REPRESENTATIVE within the 1st SELECT. When we remove and leave in this way SELECT TOTAL_PEDIDO,TOTAL_FATURAMENTO FROM returns the correct data, but it is clear without the presence of COD_REPRESENTANTE variables, REPRESENTATIVE. Any idea how we can solve this??
– Adair Juneo
As they are equal, unless they do not exist in one of the tables, you can use:
COALESCE(foo.COD_REPRESENTANTE, bar.COD_REPRESENTANTE), COALESCE(foo.rEPRESENTANTE, bar.REPRESENTANTE)
.– anonimo
Where will I apply this COALESCE function? Would that be: SELECT TOTAL_PEDIDO,TOTAL_FATURAMENTO, COALESCE(foo.COD_REPRESENTANTE, bar.COD_REPRESENTANTE), COALESCE(foo.REPRESENTATIVE, bar.REPRESENTATIVE) FROM? Or I have to apply that COALESCE below the SELECT of each SUM?
– Adair Juneo
In the most external select list:
SELECT COALESCE(foo.COD_REPRESENTANTE, bar.COD_REPRESENTANTE) COD_REPRESENTANTE, COALESCE(foo.REPRESENTANTE, bar.REPRESENTANTE) REPRESENTANTE, TOTAL_FATURAMENTO, TOTAL_PEDIDO FROM ...
– anonimo
Thank you very much brother! It worked perfect here.
– Adair Juneo