decode and sign up to get the percentage

Good evening

I'm trying to understand how to use decode and sign to return the correct percentage

for example, we have

amount1 = 3000

Amount2 = 2000

SELECT  Round(((amount1-amount2) / amount1)*100,2) from dual;

return to 33.33%

This is the reason why I need the decode.

In the case where amount1 is 0-100% will result

in the case where amount2 is 0 the result will be 100%

in the case where both amount1 and amount2 are 0 the result will be 0%

How can I achieve this?

Thank you for your time

Hello

The answer to "How can I use DECODE to...» "is 'do not use DECODE; Instead use CASES. "If you have to ask, DECODE is the wrong tool for the job.

Here's one way:

SELECT ROUND (CASE

WHEN amount1 = 0

AND amount2 = 0 THEN 0

WHEN amount1 = 0 THEN - 100

OTHER 100 * (amount1 - amount2)

/ amount1

END

2

) AS pct

FROM table_x

;

If you would care to post CREATE TABLE and INSERT statements for some sample data and the results that you want from this data, then I could test this.

Tags: Database

Similar Questions

Maybe you are looking for