HOW TO ORDER BY COLUMN NICKNAME

Asked

Viewed 56 times

-3

Good morning Everyone, I’m trying to give a Order By by the column of a case nicknamed PROVISION but it won’t.

SELECT
 CODIGO,
 PRODUTO,
 VENDIDO,
 ESTOQUE,
 PERIODO || ' DIAS' AS PERIODO,

(CASE WHEN VENDIDO = 0 THEN 0
 ELSE 
(CASE WHEN VENDIDO > 0 THEN
       ROUND(VENDIDO/PERIODO,2)
 END)END) AS "MÉDIA P/ DIA",
 
 
(CASE WHEN VENDIDO = 0 OR ESTOQUE = 0 THEN 0
 ELSE
(CASE WHEN VENDIDO > 0 AND ESTOQUE > 0 THEN
       ROUND((ESTOQUE) / (VENDIDO/PERIODO),0)
 END) END) || '  dias' AS **"PROVISAO"**   
 
 
 FROM(
 SELECT 
 P.CODPROD CODIGO,
 P.DESCRICAO PRODUTO,
 NVL(SUM(PCPEDI.QT),0) VENDIDO,
 PCEST.QTEST - PCEST.QTRESERV ESTOQUE,
 
 (TO_DATE(:FIM) - TO_DATE(:INICIO)+1) AS "PERIODO"
 
 FROM PCPRODUT P
 
 LEFT JOIN PCPEDI ON P.CODPROD = PCPEDI.CODPROD
      AND PCPEDI.DATA BETWEEN :INICIO AND :FIM
 JOIN PCEST ON PCEST.CODPROD = P.CODPROD     
      
WHERE P.CODFORNEC = '23'
AND P.OBS2 <> 'FL'
                   

GROUP BY
P.CODPROD,
P.DESCRICAO,
PCEST.QTEST,
PCEST.QTRESERV

**ORDER BY
PROVISAO ASC** 

)
  • One possibility is you use the field number in the selection list in the case: ORDER BY 7 ASC.

1 answer

-2


    WITH myTable AS (
SELECT
 CODIGO,
 PRODUTO,
 VENDIDO,
 ESTOQUE,
 PERIODO || ' DIAS' AS PERIODO,

(CASE WHEN VENDIDO = 0 THEN 0
 ELSE 
(CASE WHEN VENDIDO > 0 THEN
       ROUND(VENDIDO/PERIODO,2)
 END)END) AS "MÉDIA P/ DIA",
 
 
(CASE WHEN VENDIDO = 0 OR ESTOQUE = 0 THEN 0
 ELSE
(CASE WHEN VENDIDO > 0 AND ESTOQUE > 0 THEN
       ROUND((ESTOQUE) / (VENDIDO/PERIODO),0)
 END) END) || '  dias' AS **"PROVISAO"**   
 
 
 FROM(
 SELECT 
 P.CODPROD CODIGO,
 P.DESCRICAO PRODUTO,
 NVL(SUM(PCPEDI.QT),0) VENDIDO,
 PCEST.QTEST - PCEST.QTRESERV ESTOQUE,
 
 (TO_DATE(:FIM) - TO_DATE(:INICIO)+1) AS "PERIODO"
 
 FROM PCPRODUT P
 
 LEFT JOIN PCPEDI ON P.CODPROD = PCPEDI.CODPROD
      AND PCPEDI.DATA BETWEEN :INICIO AND :FIM
 JOIN PCEST ON PCEST.CODPROD = P.CODPROD     
      
WHERE P.CODFORNEC = '23'
AND P.OBS2 <> 'FL'
                   

GROUP BY
P.CODPROD,
P.DESCRICAO,
PCEST.QTEST,
PCEST.QTRESERV


)
);

SELECT * FROM myTable ORDER BY PROVISAO ASC
  • PERFECT ! Thank you very much Marcos

Browser other questions tagged

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