Totals and Subtotals in Object Oriented ALV Report
The class used for getting the Total of any numeric column is CL_SALV_AGGREGATIONS.
To get the aggregation or total value of any column we have to get aggregations object from ALV instance and then call the add aggregation method by specifying the column name.
To get the subtotal we have to get sorting object (GET_SORTS) from ALV instance and we need to sort the column on which we need a subtotal. For that we must use the class CL_SALV_SORTS and pass the flag Subtotal = True.
The below example shows the SPFLI table records in ALV with Total Distance and Subtotal distance for each CityTo.
REPORT ztest_ppk. DATA: lo_alv TYPE REF TO cl_salv_table, lo_aggrs TYPE REF TO cl_salv_aggregations, lo_sort TYPE REF TO cl_salv_sorts. START-OF-SELECTION. SELECT * FROM spfli INTO TABLE @DATA(lt_spfli). IF sy-subrc IS INITIAL. *1. GET ALV INSTANCE. cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv CHANGING t_table = lt_spfli[] ). *2. Specify the Column for Aggregation lo_aggrs = lo_alv->get_aggregations( ). lo_aggrs->add_aggregation( EXPORTING columnname = 'DISTANCE' " Aggregation Field Name aggregation = if_salv_c_aggregation=>total " Aggregation ). *3. Sort the field on which Subtotal is required. *-- and pass Subtotal = True to display the subtotals. lo_sort = lo_alv->get_sorts( ). lo_sort->add_sort( EXPORTING columnname = 'CITYTO' " ALV Control: Field Name of Internal Table Field subtotal = if_salv_c_bool_sap=>true RECEIVING value = lo_sort_column " ALV Sort Settings ). *4. DIPLAY ALV OUTPUT lo_alv->display( ). ENDIF.