0
Hello.
The Code below sometimes does not run because it is presented the error: You cannot divide by zero. I tried to do with a case, but my code is too big and the case would leave the structure slow and bad to treat in eventual "maintenance".
Example: If the divisor is zero, then replace with 1 so that it returns only the dividend.
SELECT
(SELECT 8 FROM DUAL) AS ORDEM,
(SELECT 'DESC MEDIO REP %' FROM DUAL) AS DESCRICAO,
ROUND((1-(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2017, MRG.VLRTOT,0))/COALESCE(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2017, MRG.PRTAB,0)),1)))*100,2) AS DESC_2017,
ROUND((1-(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2018, MRG.VLRTOT,0))/COALESCE(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2018, MRG.PRTAB,0)),1)))*100,2) AS DESC_2018,
ROUND((1-(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2019, MRG.VLRTOT,0))/COALESCE(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2019, MRG.PRTAB,0)),1)))*100,2) AS DESC_2019,
ROUND((1-(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2020, MRG.VLRTOT,0))/COALESCE(SUM(DECODE(TO_CHAR(MRG.DTENTSAI, 'YYYY'), 2020, MRG.PRTAB,0)),1)))*100,2) AS DESC_2020,
CASE WHEN 1 = 1 THEN '#ffd5c6' END BKCOLOR
FROM AD_DADOSMRG_EX MRG
WHERE
MRG.TIPMOV IN ('V', 'D') AND
MRG.GRUPO IN ('VENDAS') AND
MRG.CODVEND = (SELECT PAR.CODVEND FROM TGFPAR PAR WHERE PAR.CODPARCMATRIZ = &CODPARCMATRIZ AND PAR.CODVEND NOT IN 7945 AND ROWNUM = 1)
GROUP BY 1
Use the function GREATEST. Ex:
GREATEST(COALESCE...,1);
– Bruno Warmling
It worked rsrs, thank you
– Rodrigo Macaúbas
Only take care of one thing, if the value of your COALESCE is 0.xxxxx, will get 1... Otherwise only to solve even with CASE or DECODE.
– Bruno Warmling