SAP CDS AVG AS V2



Get Example source ABAP code based on a different SAP table
  


• AVG, AS ABAP_CDS_EXPRESSION

ABAP_CDS_DDL - CDS View Entity, AVG, AS

ABAP_SYNTAX
... AVG( $[DISTINCT$] arg AS dtype ) ...

What does it do?
Aggregate expression AVG of a CDS view entity specifying the data type dtype. The data type determines the result type in which the average value is returned.
The same can be specified for arg as for all aggregate expressions. The data type of arg must be numeric and have one of the types INT1, INT2, INT4, INT8, FLTP, DEC, CURR, QUAN, DECFLOAT16, and DECFLOAT34.
The data type dtype can have the built-in numeric type DEC, CURR, QUAN, DECFLOAT16, DECFLOAT34, or FLTP.
The data type dtype can be specified as follows:
Using a CDS simple type with an appropriate data type.
Using a DDIC data element with an appropriate data type.
Directly as a built-in data type in ABAP Dictionary.
The following table shows the syntax for specifying built-in data types: <(>dtype<)>Dictionary Type
abap.dec(len,decimals) DEC with length len and decimals decimal places
abap.curr(len,decimals) CURR with length len and decimals decimal places
abap.quan(len,decimals) QUAN with length len with decimals decimal places
abap.decfloat16$[(len,decimals)$] DECFLOAT16 with length len with decimals decimal places
abap.decfloat34$[(len,decimals)$] DECFLOAT34 with length len with decimals decimal places
abap.fltp$[(16,16)$]FLTP
The value range of the data type dtype must cover the value range of the type of arg:
If arg is an integer type, a type dtype of type DEC or QUAN must have at least as many integer digits as is required by the value range of the respective integer type, that is, 3 for INT1 , 5 for INT2, 10 for INT4, and 19 for INT8. There is no restriction on the number of decimal places.
If arg has type DEC or QUAN, then dtype of type DEC or QUAN must have at least as many integer digits and decimal places.
If arg has the data type DECFLOAT16, then dtype must be DECFLOAT16 or DECFLOAT34 . If arg has the data type DECFLOAT34, then the target data type dtype must also be DECFLOAT34 .
If arg has type FLTP, this type must also be specified for dtype.
If arg has type CURR, this type must also be specified for dtype and the number of decimal places must match exactly.
If arg has any other type except CURR and dtype is specified as CURR, then dtype must have exactly two decimal places.
If the average value cannot be represented exactly in the data type dtype, the result is rounded commercially.
If arg is assigned a currency key using the annotation Semantics.amount.currencyCode , then the result field must also be assigned a currency key using the same annotation.
If arg is assigned a unit of measurement key using the annotation Semantics.quantity.unitofMeasure , then the result field must also be assigned a unit of measurement key using the same annotation.



Latest notes:

The addition AS to AVG bridges the gap for average values where FLTP cannot be cast to other numeric data types with CAST.
Since average values usually have decimal places, integer types are not supported for dtype. If necessary, the type DEC can be used without decimal places.
An aggregate expression AVG with the addition AS dtype can be used in accordance with this type in a CAST expression.
The actual calculation of the average value (determined by the involved data types) is platform-dependent. This can produce underflows, overflows, and exceptions during the calculation. To prevent exceptions, the total of the values for which the average value is being calculated should be within the value range of dtype. For further details on the calculation on SAP HANA Platform, see the SAP HANA SQL Reference Guide.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS view entity demonstrates different uses of the aggregate expression AVG. The class CL_DEMO_CDS_AVG_VE accesses the view. The average value is calculated with and without addition DISTINCT and is returned in various types. A built-in numeric function is used as operand to demonstrate the nesting of built-in functions.
DDLS DEMO_CDS_AVG_VE
ABAP_EXAMPLE_END