Arithmetic Expressions
Syntax
add_expression ::= multiply_expression (("+"|"-") multiply_expression)*
multiply_expression ::= unary_expression (("*"|"/"|"div") unary_expression)*
unary_expression ::= path_expression
| (("+"|"-") unary_expression)
Semantics
Oracle NoSQL Database supports the following arithmetic operations: +, -, *, / and div. Each operand to these operators must produce at most one numeric item. If any operand returns the empty sequence or NULL, the result of the arithmetic operation is also empty or NULL, respectively. Otherwise, the operator returns a single numeric item, which is computed as follows:
- If any operand returns a Number item, the item returned by the other operand is cast to a Number value (if not a Number already) and the result is a Number item that is computed using java's arithmetic on BigDecimal, otherwise,
- If any operand returns a double item, the item returned by the other operand is cast to a double value (if not a double already) and the result is a double item that is computed using java's arithmetic on doubles, otherwise,
- If any operand returns a float item, the item returned by the other operand is cast to a float value If not a float already) and the result is a float item that is computed using java's arithmetic on floats, otherwise,
- Except for the div operator, if any operand returns a long item, the item returned by the other operand is cast to a long value (if not a long already) and the result is a long item that is computed using java's arithmetic on longs.
- Except for the div operator, if all operands return integer items, the result is an integer item that is computed using java's arithmetic on ints.
- The div operator performs floating-point division, even if both its operands are longs and/or integers. In this case, div returns a double.
Oracle NoSQL Database supports the unary + and – operators as well. The unary + is a no-op, and the unary – changes the sign of its numeric argument.
Example 6-85 Arithmetic Expression
For each user show their id and the difference between their actual income and an income that is computed as a base income plus an age-proportional amount.
DECLARE
$baseIncome INTEGER;
$ageMultiplier DOUBLE;
SELECT id,
income - ($baseIncome + age * $ageMultiplier) AS adjustment
FROM Users;