Division of SUM() into SQL Developer

Asked

Viewed 85 times

0

We are running the SELECT below in order to deliver a percentage difference between the somatic values PRECO_UNITION and PRECO_TABELA. We try to do this in the simplest way by performing the common division within SELECT and assigning DIFERENCA_PERCENTUAL, but Sql Developer delivers the following error:

ORA-00904: "DIFERENCA_PERCENTUAL": identificador inválido
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Erro na linha: 14 Coluna: 68

Below is the SELECT we are running on SQL Developer:

SELECT
FAITEMPE.NF NOTA_FISCAL,
FAITEMPE.CD_EMPRESA COD_CLIENTE,
GEEMPRES.NOME_COMPLETO NOME_CLIENTE,
SUM(FAITEMPE.PR_UNITARIO) AS PRECO_UNITARIO,
SUM(FAITEMPE.PR_ORIGINAL) AS PRECO_TABELA,
(PR_UNITARIO / PR_ORIGINAL) AS DIFERENCA_PERCENTUAL

FROM FAITEMPE, GEEMPRES
WHERE FAITEMPE.CD_EMPRESA = GEEMPRES.CD_EMPRESA
AND FAITEMPE.DT_ITEM BETWEEN TO_DATE(:data_inicial, 'DD/MM/YYYY') AND TO_DATE(:data_final, 'DD/MM/YYYY')
AND FAITEMPE.CONTROLE LIKE '16'

GROUP BY FAITEMPE.NF, FAITEMPE.CD_EMPRESA, GEEMPRES.NOME_COMPLETO, DIFERENCA_PERCENTUAL

We also forward the result of the simple query before incrementing this DIFERENCA_PERCENTUAL:

Resultado da consulta select sem a intenção de cálculo dessa diferenca_percentual

Any better suggestions on how we can extract this information???

  • 2

    ( SUM(FAITEMPE.PR_UNITARIO) /SUM(FAITEMPE.PR_ORIGINAL) as

  • @Motta worked well here. Thanks man!

1 answer

2


Publishing and improving this solution

...
(CASE WHEN SUM(FAITEMPE.PR_ORIGINAL) <> 0 
       THEN ( SUM(FAITEMPE.PR_UNITARIO) /
              SUM(FAITEMPE.PR_ORIGINAL) ) 
 ELSE NULL END) as DIFERENCA_PERCENTUAL,
...

Case being for error handling only.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.