ABAP - Keyword Documentation →  ABAP - Programming Language →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Read Access →  SELECT, clauses →  SELECT, GROUP BY → 
Mail Feedback

sql_agg - GROUPING

Syntax

... GROUPING( col )

Effect

The result of the grouping function GROUPING indicates whether a specific column is part of the aggregation. After the grouping function GROUPING, a column name in parentheses is expected as an argument.

The return value of the grouping function GROUPING has the data type INT1. The following return values of the grouping function are possible here:

Value Meaning
0 The column is not part of the aggregation.
1 The column is part of the aggregation.

The columns that are part of the aggregation contain the null values as placeholders in the corresponding results. More specifically, the grouping function GROUPING determines whether the column that contains a null value in the result was part of the aggregation or whether it was already the null value in the data source.

Rules

Example

The following example calculates the number of Lufthansa flights depending on the plane type (the column planetype) and the connection (the column connid). Here, the grouping function for the plane type column or connection column is used to determine whether they are part of the aggregation.

SELECT FROM sflight
       FIELDS carrid,
              connid,
              planetype,
              COUNT( * ) AS flights_count,
              grouping( connid ) AS agg_connid,
              grouping( planetype ) AS agg_planetype
       WHERE carrid = 'LH'
       GROUP BY GROUPING SETS ( ( carrid, planetype ),
                                ( carrid, connid ),
                                (  ) )
       ORDER BY connid, planetype
       INTO TABLE @FINAL(result_grouping).

cl_demo_output=>display( result_grouping ).

Executable Example

GROUPING, Aggregate Function