ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - result → 

SELECT - aggregate

Short Reference

Syntax

... { MAX( [DISTINCT] col )
    | MIN( [DISTINCT] col )
    | AVG( [DISTINCT] col )
    | SUM( [DISTINCT] col )
    | COUNT( DISTINCT col )
    | COUNT( * )
    | COUNT(*) } ... .


Effect

Any number of the specified column identifiers can be included in the SELECT statement as arguments of the aggregate expressions above. In aggregate expressions, a single value is calculated in multiple rows from the values of one column as follows (note that the addition DISTINCT excludes double values in the calculation):

  1. MAX( [DISTINCT] col ) determines the maximum value of the value of column col in the results set or in the current group.

  2. MIN( [DISTINCT] col ) determines the maximum value of the contents of column col in the results set or in the current group.

  3. AVG( [DISTINCT] col ) determines the average value of the contents of column col in the results set or in the current group. The data type of the column must be numeric. The data types DF16_RAW, DF16_SCL, DF34_RAW, and DF34_SCL are not permitted.

  4. SUM( [DISTINCT] col ) determines the sum of the contents of column col in the results set or in the current group. The data type of the column must be numeric. The data types DF16_RAW, DF16_SCL, DF34_RAW, and DF34_SCL are not permitted.

  5. COUNT( DISTINCT col ) determines the number of different values in column col in the restuls set or in the current group.

  6. COUNT( * ) (or COUNT(*)) determines the number of rows in the results set or in the current group. No column identifier is specified in this case.

If aggregate expressions are used, any column identifiers that are not included as arguments of an aggregate function must be included after the GROUP BY addition. The aggregate functions evaluate the contents of the groups defined by GROUP BY in the database system and pass the result to the merged rows of the results set.

The data type of aggregate expressions with the function MAX, MIN, or SUM is the data type of the corresponding column in ABAP Dictionary. Aggregate expressions with the function AVG for decimal floating point numbers have the corresponding data type (DF16_DEC or DF34_DEC); otherwise, they have the data type FLTP. Aggregate expressions with the function COUNT have the data type INT4. The corresponding data object after INTO or APPENDING has to be selected accordingly. If the value of an aggregation expression is too large for the target area, an exception is raised. For the COUNT function in particular, numbers larger than 2,147,483,647 are not permitted.

Note the following points in relation to using aggregate expressions:

Note

If the aggregate function SUM is used for columns of type DF16_DEC, we recommend using a target field of data type decfloat34 to avoid overflows.

Example

Determines the number of airlines flying to New York.

DATA count TYPE i.

SELECT COUNT( DISTINCT carrid )
       FROM spfli
       INTO count
       WHERE cityto = 'NEW YORK'.

Example

Returns the flight date, the number of passengers, and the average and maximum luggage weight of all Lufthansa flights with the flight number 0400.

TYPES: BEGIN OF wa,
        fldate LIKE sbook-fldate,
        count  TYPE i,
        avg    TYPE p DECIMALS 2,
        max    TYPE p DECIMALS 2,
      END OF wa.
DATA   itab TYPE TABLE OF wa WITH EMPTY KEY.

SELECT fldate COUNT( * ) AVG( luggweight ) MAX( luggweight )
       FROM sbook
       INTO TABLE itab
       WHERE carrid = 'LH' AND
             connid = '0400'
       GROUP BY fldate.

cl_demo_output=>display( itab ).