Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Reads →  SELECT clauses →  SELECT - GROUP BY 

sql_agg - GROUPING

Other versions: 7.31 | 7.40 | 7.54

Syntax


 ... GROUPING( col ) 

Effect

The result of the grouping function GROUPING indicates whether a specific column is part of the aggregation. The grouping function GROUPING must be followed by a column name in parentheses 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

  • The grouping function can only be used in the SELECT list and in the HAVING clause.

  • The column used as an argument of the grouping function GROUPING must also be added to the GROUP BY list.


Example

The following example calculates the number of Lufthansa flights by plane type (the column planetype) and by 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 @DATA(result_grouping). 

cl_demo_output=>display( result_grouping ).

Executable Example

GROUPING, Aggregate Function