0
We need to perform the following queries in only 1 select:
1st SELECT
SELECT SUM(FAPEDIDO.TOTAL_PEDIDO) AS TOTAL_CAR,
FAPEDIDO.CD_REPRESENTANT COD_REPRESENTANTE,
GEEMPRES.NOME_COMPLETO REPRESENTANTE
FROM FAPEDIDO,GEEMPRES
WHERE FAPEDIDO.CD_REPRESENTANT = GEEMPRES.CD_EMPRESA
AND FAPEDIDO.CONTROLE LIKE '02'
AND FAPEDIDO.DT_PEDIDO BETWEEN TO_DATE(:data_posicao_sintetica_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_posicao_sintetica_final, 'DD/MM/YYYY')
GROUP BY GEEMPRES.NOME_COMPLETO,FAPEDIDO.CD_REPRESENTANT
2nd SELECT
SELECT SUM(FANFISCA.TOTAL_NF) AS TOTAL_FAT,
FANFISCA.CD_REPRESENTANT COD_REPRESENTANTE,
GEEMPRES.NOME_COMPLETO REPRESENTANTE
FROM FANFISCA,GEEMPRES
WHERE GEEMPRES.CD_EMPRESA = FANFISCA.CD_REPRESENTANT
AND FANFISCA.DT_EMISSAO BETWEEN TO_DATE(:data_posicao_sintetica_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_posicao_sintetica_final, 'DD/MM/YYYY')
AND FANFISCA.ESPECIE_NOTA LIKE 'S'
AND FANFISCA.CFOP BETWEEN '5101' AND '6404'
GROUP BY FANFISCA.CD_REPRESENTANT, GEEMPRES.NOME_COMPLETO
3rd SELECT
SELECT SUM(FAPEDIDO.TOTAL_PEDIDO) AS TOTAL_PED,
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'
AND FAPEDIDO.DT_PEDIDO BETWEEN TO_DATE(:data_posicao_sintetica_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_posicao_sintetica_final, 'DD/MM/YYYY')
GROUP BY GEEMPRES.NOME_COMPLETO, FAPEDIDO.CD_REPRESENTANT
We need to return a query in table this way:
ANY IDEA HOW WE CAN DESIGN THIS SELECT TO RETURN TO THE TABLE?
The query worked but returned only 1 representative, below a return print. https://imgur.com/KEHC4fs Any idea how we can return the full query?
– Adair Juneo
Pe4squise for the difference between the different types of JOIN. Considering that these 0 indicate the absence of this COD_REPRESENTANTE in the associated table FULL OUTER JOIN, or even LEFT OUTER JOIN must meet (do any of these values always exist?). One remark: UNION defines an SQL operation that is not the one you entered you want.
– anonimo
Adair, all queries have no results for the representative, in case use LEFT JOIN instead of INNER JOIN, I will change what I did for you.
– Heitor Scalabrini
Changed the answer I gave you for LEFT JOIN, with explanation of why use LEFT JOIN
– Heitor Scalabrini
As I saw it seems that everyone has the invoice, in case the invoice result should belong to the first subselect, due to the structure of the left Join, but as our anonymous friend said, maybe the best option is the OUTER JOIN, but the right one is to use the table that contains more data in the first subslect (in this case that contains all representatives) or the own table of representatives.
– Heitor Scalabrini
I edited with the right left Join.
– Heitor Scalabrini
Guys, thank you very much, it worked perfect! Vlw msm for the help, now I could understand the difference between these operators.
– Adair Juneo