SALV Table 11 – Apply Aggregations

By | December 22, 2009 | SALV Table, SALV Tutorial, Tutorials | 30,432 | 9

Apply Aggregations like Totals, Minimum values to SALV Table

In the series of the SALV Model Table display in SAP ABAP, today we will see how to apply aggregations to the ALV Grid. You can find all the Previous discussion at Tutorials > SALV Table Display .

Aggregations plays important role in the reporting. E.g. Providing the TOTALS, Minimum values, Maximum values, Subtotals etc. To achieve this in the SALV model, we have the class CL_SALV_AGGREGATIONS.

For instance, to apply TOTALS on the the NETWR column (as shown in the example) we need to use the method ADD_AGGREGATION on this column. The parameter AGGREGATION would be used to determine the type of the aggregation. Interface IF_SALV_C_AGGREGATION contains the constants which can be used in this parameter.

In this example I need to change the output table to add column NETWR as compared to previous discussion in this series, so I will provide the entire code which generate the output which has all three scenario. For test purpose, We will apply TOTALS on the NETWR column. Once the TOTAL is applied, we will bring the TOTAL row at the top.

REPORT  ztest_oo_alv_aggr.
*
*----------------------------------------------------------------------*
*       Code to apply Aggregations to SALV Table display
*----------------------------------------------------------------------*
*       CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
  PUBLIC SECTION.
*
*   Final output table
    TYPESBEGIN OF ty_vbak,
           vbeln     TYPE vbak-vbeln,
           erdat     TYPE erdat,
           auart     TYPE auart,
           kunnr     TYPE kunnr,
           netwr     TYPE netwr,
           END   OF ty_vbak.
    TYPESty_t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
    DATAt_vbak TYPE STANDARD TABLE OF ty_vbak.
*
*   ALV reference
    DATAo_alv TYPE REF TO cl_salv_table.
*
    METHODS:
*     data selection
      get_data,
*
*     Generating output
      generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
*    In this section we will define the private methods which can
*      be implemented to set the properties of the ALV and can be
*      called in the
*
  PRIVATE SECTION.
    METHODS:
      set_pf_status
        CHANGING
          co_alv TYPE REF TO cl_salv_table.
*
    METHODS:
      set_aggregations
        CHANGING
          co_alv  TYPE REF TO cl_salv_table.
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
ENDCLASS.                    "lcl_report DEFINITION
*
*
START-OF-SELECTION.
  DATAlo_report TYPE REF TO lcl_report.
*
  CREATE OBJECT lo_report.
*
  lo_report->get_data).
*
  lo_report->generate_output).
*
*----------------------------------------------------------------------*
*       CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
  METHOD get_data.
*   data selection
    SELECT vbeln erdat auart kunnr netwr
           INTO  CORRESPONDING FIELDS OF TABLE t_vbak
           FROM  vbak
           UP TO 20 ROWS.
*
  ENDMETHOD.                    "get_data
*
*.......................................................................
  METHOD generate_output.
* New ALV instance
*   We are calling the static Factory method which will give back
*   the ALV object reference.
*
* exception class
    DATAlx_msg TYPE REF TO cx_salv_msg.
    TRY.
        cl_salv_table=>factory(
          IMPORTING
            r_salv_table o_alv
          CHANGING
            t_table      t_vbak ).
      CATCH cx_salv_msg INTO lx_msg.
    ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
*    In this area we will call the methods which will set the
*      different properties to the ALV
*
*   Set default PF status
    CALL METHOD set_pf_status
      CHANGING
        co_alv o_alv.
*
*   Set the colors to ALV display
    CALL METHOD set_aggregations
      CHANGING
        co_alv o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* Displaying the ALV
*   Here we will call the DISPLAY method to get the output on the screen
    o_alv->display).
*
  ENDMETHOD.                    "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
*    In this area we will implement the methods which are defined in
*      the class definition
*
*
  METHOD set_pf_status.
*
    DATAlo_functions TYPE REF TO cl_salv_functions_list.
*
    lo_functions co_alv->get_functions).
    lo_functions->set_defaultabap_true ).
*
  ENDMETHOD.                    "set_pf_status
*
  METHOD set_aggregations.
*
    DATAlo_aggrs TYPE REF TO cl_salv_aggregations.
*
    lo_aggrs co_alv->get_aggregations).
*
*   Add TOTAL for COLUMN NETWR
    TRY.
        CALL METHOD lo_aggrs->add_aggregation
          EXPORTING
            columnname  'NETWR'
            aggregation if_salv_c_aggregation=>total.
      CATCH cx_salv_data_error .                        "#EC NO_HANDLER
      CATCH cx_salv_not_found .                         "#EC NO_HANDLER
      CATCH cx_salv_existing .                          "#EC NO_HANDLER
    ENDTRY.
*
*   Bring the total line to top
    lo_aggrs->set_aggregation_before_items).
*
  ENDMETHOD.                    "set_aggregations
*
*
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
ENDCLASS.                    "lcl_report IMPLEMENTATION

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

9 Comments

  • Very nice! Now I need to read teh first part of the tutuorial.

    Thanks!

  • Anonymous

    Hi Naimesh,

    Is there any way that the grandtotal line could be hidden while displaying the subtotals ?

  • Anonymous

    Hi Naimesh,
    I need to hide grandtotal line which is shown with subtotals.

    how can i achieve this?

  • Anonymous

    Hi Naimesh,
    I want to schedule this report in background and still get the changes i did for my subtotals. Can you help out to code this ?

    Thanks.

  • Anonymous

    Hi Naimesh,
    Is there a way for the classical ALV grid report ("Classical ALV: Change Subtotal – II for Print") to schedule in background and still have the subtotal changes in there ? can you help me code.

    Thanks

  • Hello,

    I will do some research to make it work and post it solution, if I come across any.

    Regards,
    Naimesh Patel

  • Anonymous

    Hello Naimesh,
    Thanks for replying. I have a requirement where i have to provide 7 subtotals with % which i have been successful using this code. But when the program was schedule it was a big boo-boo. It would be of great help if you can help me solve this.

    Thanks.

  • Anonymous

    This is done your blog was very helpful to resolve this issue.

  • fleischr

    Excellent series!

    Is it possible to aggregate purely on the number of rows grouped together? I’ve found so far the only way I could do this is to add a column with ‘1’ and aggregate that.

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.