SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Accesses → Open SQL → Open SQL - Read Accesses → SELECT → SELECT - result →
SELECT - aggregate
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):
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 ).