I faced with obscure code on PL/SQL in my job, please, could you explain for me, what does that mean?
PL/SQL execute 'select' request e.g.
DECODE(SIGN(ABS(SUM(amount))) - :3, -1, 0, SUM(AMOUNT)),
What is the ' - :3'? What is the function of?
I faced with obscure code on PL/SQL in my job, please, could you explain for me, what does that mean?
PL/SQL execute 'select' request e.g.
DECODE(SIGN(ABS(SUM(amount))) - :3, -1, 0, SUM(AMOUNT)),
What is the ' - :3'? What is the function of?
This can be used as a good example of how to break down a confusing line of code to figure out what is going on. You didn't include the entire query, so some things will have to be taken out of context. Firstly, add some spaces to clarify the individual nested functions and make it more readable for now:
DECODE( SIGN( ABS( SUM(amount) ) ) - :3, -1, 0, SUM(AMOUNT) ),
Then analyze them inner-most first to understand what is going on:
DECODE( SIGN( ABS( **SUM(amount)** ) ) - :3, -1, 0, SUM(AMOUNT) ),
First sum the column called "amount" that is part of the query that we don't see
DECODE( SIGN( **ABS( SUM(amount) )** ) - :3, -1, 0, SUM(AMOUNT) ),
Then get it's absolute value (removes the sign)
DECODE( **SIGN( ABS( SUM(amount) ) )** - :3, -1, 0, SUM(AMOUNT) ),
Then return a value indicating the sign. If < 0 return -1, or if = 0 then return 0 and if > 0 return 1. It can only ever be 0 or 1 due to the ABS() function though. In effect, it seems to be testing if the value is 0 or not.
DECODE( **SIGN( ABS( SUM(amount) ) ) - :3**, -1, 0, SUM(AMOUNT) ),
From that, subtract whatever value was passed in the 3rd bind variable (others have given info on bind variables). This is the expression that will be tested in the decode statement.
If the value is -1, return 0, else return the sum of the amount column.
Some things don't seem to make sense, like getting the absolute value which strips the sign, then calling the sign() function. However, without specs and the full select which would show what type of value :3 is it is hard to know what the original intention was. I suspect this is why you are here, as you are the lucky one tasked with figured out why this is not working as expected. As you figure it out, add to a comment in the code to both clarify your thinking and to help the next person.
Good luck!