ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Reads →  SELECT clauses →  SELECT - select_clause →  SELECT - select_list →  SELECT - col_spec →  SELECT - aggregate → 

SELECT - AVG, AS

Syntax

... AVG( [DISTINCT] col AS dtype ) ...

Effect

Aggregate expression AVG in Open SQL with specification of a data type dtype. The data type determines the result type in which the average value is returned. The data type of col must be numeric and have one of the types INT1, INT2, INT4, INT8, DEC, CURR, QUAN, or FLTP. The addition AS cannot yet be used for the types DF16_..., DF34_... for decimal floating point numbers.

The data type dtype can have the built-in numeric type DEC, CURR, QUAN, or FLTP. The data type FLTP can only be specified for operands of type FLTP. The data type dtype can be specified as follows:

Length and decimal places must be specified with len and decimals. Literals or constant host variables of the ABAP type b, s, i, or int8 can be specified for len and decimals. The value range of the data type dtype must cover the value range of the operand type:

If the average value cannot be represented exactly in the data type dtype, the result is rounded commercially.

Notes

Example

Apply the aggregate function AVG to a column of the database table DEMO_EXPRESSIONS. The program DEMO_SQL_AVG executes this access to the table and represents the result.

SELECT
  FROM demo_expressions
  FIELDS
    AVG(          num1                ) AS avg_no_type,
    AVG( DISTINCT num1                ) AS avg_no_type_distinct,
    AVG(          num1 AS DEC( 10,0 ) ) AS avg_dec0,
    AVG( DISTINCT num1 AS DEC( 10,0 ) ) AS avg_dec0_distinct,
    AVG(          num1 AS DEC( 14,4 ) ) AS avg_dec4,
    AVG( DISTINCT num1 AS DEC( 14,4 ) ) AS avg_dec4_distinct
  INTO @DATA(result).