SELECT - aggregate
Short Reference
Syntax
... { MAX( [DISTINCT] col )
| MIN( [DISTINCT] col )
| AVG( [DISTINCT] col )
| SUM( [DISTINCT] col )
| COUNT( DISTINCT col )
| COUNT( * )
| count(*) } ... .
Effect
As many of the specified column labels as you like can be listed in the
SELECT command as arguments of the above aggregate expression. In aggregate expressions,
a single value is calculated from the values of multiple rows in a column as follows (note that the addition DISTINCT excludes double values from the calculation):
-
MAX( [DISTINCT] col
) Determines the maximum value of the value in the column col in the resulting set or in the current group.
-
MIN( [DISTINCT] col
) Determines the minimum value of the content of the column col in the resulting set or in the current group.
-
AVG( [DISTINCT] col
) Determines the average value of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
-
SUM( [DISTINCT] col
) Determines the sum of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
-
COUNT( DISTINCT col
) Determines the number of different values in the column col in the resulting set or in the current group.
-
COUNT( * ) (or count(*))
Determines the number of rows in the resulting set or in the current group. No column label is specified in this case.
If you are using aggregate expressions, all column labels that are not listed as an argument of an aggregate
function are listed after the addition GROUP BY. The aggregate functions evaluate the content of the groups defined by
GROUP BY in the database system and transfer the result to the combined rows of the resulting set.
The data type of aggregate expressions with the function MAX,
MIN or SUM
is the data type of the corresponding column in the ABAP Dictionary. Aggregate expressions with the
function AVG have the data type FLTP, and those with COUNT
have the data type INT4. The corresponding data object after INTO or APPENDING has to be selected accordingly.
Note the following points when using aggregate expressions:
-
If the addition FOR ALL ENTRIES is used in front of WHERE, or if
cluster or
pool tables are listed after FROM, no other aggregate expressions apart from COUNT( *
) can be used.
-
Columns of the type STRING or RAWSTRING cannot be used with aggregate functions.
-
When aggregate expressions are used, the SELECT command makes it unnecessary to use SAP buffering.
-
Null values are not included in the calculation
for the aggregate functions. The result is a null value only if all the rows in the column in question contain the null value.
-
If only aggregate expressions are used after SELECT, the
results set has one row and the addition GROUP BY is not
necessary. If a non-table type target area is specified after INTO,
the command ENDSELECT cannot be used together with the addition SINGLE. If the aggregate expression count( *
) is not being used, an internal table can be specified after INTO, and the first row of this table is filled.
-
If aggregate functions are used without GROUP BY being
specified at the same time, the resulting set also contains a row if no data is found in the database. If count( *
) is used, the column in question contains the value 0. The columns in the other aggregate
functions contain initial values. This row is assigned to the data object specified after INTO, and unless count( *
) is being used exclusively, sy-subrc is set
to 0 and sy-dbcnt is set to 1. If
count( *) is used exclusively, the addition INTO
can be omitted and if no data can be found in the database, sy-subrc is set to 4 and sy-dbcnt is set to 0.