SAP CDS ARIT EXP CALC V2



Get Example source ABAP code based on a different SAP table
  



ABAP_CDS_DDL - CDS View Entity arith_expr, Amounts andQuantities

ABAP_SYNTAX
... $[-$]operand1 $[+$|-$|*$|/ $[-$]operand2 $[+$|-$|*$|/ $[-$]operand3 ... $]$] ...

What does it do?
Arithmetic expression in a SELECT statement of a CDS view entity that operates with CDS amount fields , CDS quantity fields, and calculated quantities. The possible operators are as follows: OperatorMeaning
+Adds the operands
-Subtracts the right operand from the left
*Multiplies the operands
/Divides the left operand by the right
A minus sign (-) before an operand multiplies the operand by -1. The operands must be CDS amount fields, CDS quantity fields, CDS calculated quantities, or numeric values of one of the following data types: INT1, INT2 , INT4, INT8, DEC, DECFLOAT16 , DECFLOAT34, FLTP, or QUAN. The operands can be specified as follows:
CDS amount fields and CDS quantity fields must be fields of a data source data_source of the current CDS view entity or SQL-based scalar functions with a suitable return value.
CDS calculated quantities can be fields of a data source data_source of the current CDS view entity, reuse expressions using $projection, or SQL-based scalar functions with a suitable return value.
Numeric values can be specified as literals, fields of a data source data_source of the current CDS view entity, parameters, path expressions, built-in functions , SQL-based scalar functions, casting expressions, case distinctions, aggregate expressions , other arithmetic expressions, or reuse expressions using $projection. The data type must be appropriate.
The subexpressions of an arithmetic expression can be placed in parentheses (...).
The following table shows how amount fields and quantity fields can be used with different operators and the type of the result: operands / operators/* +, -
amount, amountcalculated quantitycalculated quantity amount
quantity, quantitycalculated quantity calculated quantity quantity
calculated quantity, amountcalculated quantity calculated quantity <(>amount<)>
amount, quantitycalculated quantitycalculated quantity -
calculated quantity, number calculated quantity calculated quantity -
calculated quantity, quantitycalculated quantity calculated quantity quantity
calculated quantity, calculated quantity calculated quantity lbr calculated quantitycalculated quantity
amount, numberamount amount-
number, amountcalculated quantity amount-
quantity, numberquantity quantity-
number, quantitycalculated quantity quantity -
Regarding the data types of the operands and of the result, the rules for arithmetic expressions apply, see topic TITLE . If the result is a calculated quantity, then the data type is always DECFLOAT34.
Note the following special conditions:
If the result of an arithmetic expression is a CDS amount field, CDS quantity field, or a calculated quantity, then it is mandatory to assign a currency key, unit key, or calculated unit using the respective Semantics annotation. If no reference is assigned, a syntax check error occurs. References are not inherited or derived from the operands of the arithmetic expression.
The following rules apply to CDS amount fields of data type CURR :
In general, CDS amount fields of data type CURR cannot be used directly as operands of arithmetic expressions. They need to be converted to data type DECFLOAT34 first, using the conversion function CURR_TO_DECFLOAT_AMOUNT .
There is only one exception to this rule: if all operands are of data type CURR and have exactly two decimal places, then they can be used in additions and subtractions (operators + and -).
A calculated quantity can be changed back to an amount or quantity field using the function GET_NUMERIC_VALUE. Then, the required reference can be added using the required annotation, Semantics.amount.currencyCode or Semantics.quantity.unitofMeasure.
If one of the operands is of type FLTP, then it is a binary floating point expression, in which all operands must be of type FLTP.
When a division is performed with the operator /, the right operand cannot have the value 0.



Latest notes:

The following conversion functions are available for CDS amount fields and CDS currency fields: CURR_TO_DECFLOAT_AMOUNT and GET_NUMERIC_VALUE.
A calculated unit can be used as reference only once in a view. If a view contains several calculated quantity fields, each field needs a different calculated unit.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
SELECT list of a CDS view entity with amounts and quantities in arithmetic expressions:
DDLS DEMO_CDS_QUAN_ARITH
For the calculation of the field my_calcQuan, the currency field needs to be normalized using function CURR_TO_DECFLOAT_AMOUNT.
my_calcQuan is a calculated quantity of type DECFLOAT34 and it requires a calculated unit reference.
AmountAddition is a field that adds two amount fields. The result is an amount field again and a new currency key needs to be assigned.
newCalcQuan multiplies a calculated quantity with a number. A calculated unit reference is required. Each calculated unit can be used only once in a view and therefore, a new calculated unit is assigned.
ABAP_EXAMPLE_END