Downtime Announcement: Please note the SAP Community Wiki will be unavailable due to a system upgrade on Thursday, September 24th between 6 and 7 AM CEST
Skip to end of metadata
Go to start of metadata

In standard ALV function module 'REUSE_ALV_GIRD_DISPLAY' or 'REUSE_ALV_LIST_DISPLAY', they provide the standard functionality of sum totally or sum subtotally. This page is explaining how to acheive this.

1. To sum total, do it when you define the fieldcategory. In the structure of slis_fieldcat_alv, there is a component 'do_sum'. When define the fieldcategory, give the value 'X' of one field, then when display using FM 'REUSE_ALV_GIRD_DISPLAY' or 'REUSE_ALV_LIST_DISPLAY', FM will display the sum of this field in extra line.

2. To sum subtotal, first in FM 'REUSE_ALV_GIRD_DISPLAY' or 'REUSE_ALV_LIST_DISPLAY', a parameter 'it_sort' is used to sort the outputlist. In the structure slis_sortinfo_alv, a field 'subtot' is used for this function. When you define sort table, you should point out the fieldname, and give value 'X' to 'subtot'. This is point out which field is depending on to sum subtotal. Second, you should point out which field should be summed. This is the same as sum total. When you define fieldcategory, point out the fieldname and give value 'X' to 'do_sum'.

Following is the code sample 

REPORT  zyh_alv_subtotal_extra_line.

TYPE-POOLS slis.

TYPES:
  BEGIN OF ty_flight,
    carrid TYPE sflight-carrid,
    connid TYPE sflight-connid,
    fldate TYPE sflight-fldate,
    seatsmax TYPE sflight-seatsmax,
    seatsocc TYPE sflight-seatsocc,
  END OF ty_flight.

TYPES:
  ty_flight_tab TYPE STANDARD TABLE OF ty_flight.

DATA:
  it_flight TYPE ty_flight_tab.

START-OF-SELECTION.
  PERFORM sub_alv_display.

*&---------------------------------------------------------------------*
*&      Form  sub_alv_display
*&---------------------------------------------------------------------*
*       Display alv
*----------------------------------------------------------------------*
FORM sub_alv_display.
  DATA:
    li_fieldcat TYPE slis_t_fieldcat_alv,
    lw_layout TYPE slis_layout_alv,
    li_sort TYPE slis_t_sortinfo_alv,
    lw_sort TYPE slis_sortinfo_alv.

  PERFORM sub_get_data.
  PERFORM sub_form_fieldcat CHANGING li_fieldcat.
  PERFORM sub_sort CHANGING li_sort.

*------------------------------------------------------------
* List display is different from grid display. List display
* is just displaying total or subtotal, but grid display
* besides display it also provide the extension.
* Meanwhile in grid display, if sorting and the several line
* of sorting fields have same value, the same value will not
* display, only display one value
*------------------------------------------------------------
*  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
*    EXPORTING
*      I_CALLBACK_PROGRAM             = sy-repid
*      IS_LAYOUT                      = lw_layout
*      IT_FIELDCAT                    = li_fieldcat
*      IT_SORT                        = li_sort
*    TABLES
*      t_outtab                       = it_flight
*    EXCEPTIONS
*      PROGRAM_ERROR                  = 1
*      OTHERS                         = 2
*            .
*  IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*  ENDIF.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      is_layout          = lw_layout
      it_fieldcat        = li_fieldcat "can sum total
      it_sort            = li_sort "can sum sub total
    TABLES
      t_outtab           = it_flight
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


ENDFORM.                    "sub_alv_display

*&---------------------------------------------------------------------*
*&      Form  sub_get_data
*&---------------------------------------------------------------------*
*       Select data
*----------------------------------------------------------------------*
FORM sub_get_data.
  SELECT carrid
         connid
         fldate
         seatsmax
         seatsocc
    FROM sflight
    INTO TABLE it_flight
    UP TO 20 ROWS.
ENDFORM.                    "sub_get_data

*&---------------------------------------------------------------------*
*&      Form  sub_form_fieldcat
*&---------------------------------------------------------------------*
*       Define fieldcategory
*----------------------------------------------------------------------*
*      -->PI_FIELDCAT  text
*----------------------------------------------------------------------*
FORM sub_form_fieldcat CHANGING pi_fieldcat TYPE slis_t_fieldcat_alv.
  DATA:
    lw_fieldcat TYPE slis_fieldcat_alv.

  lw_fieldcat-fieldname = 'CARRID'.
  lw_fieldcat-seltext_l = 'CARRID'.
  APPEND lw_fieldcat TO pi_fieldcat.
  CLEAR lw_fieldcat.

  lw_fieldcat-fieldname = 'CONNID'.
  lw_fieldcat-seltext_l = 'CONNID'.
  APPEND lw_fieldcat TO pi_fieldcat.
  CLEAR lw_fieldcat.

  lw_fieldcat-fieldname = 'FLDATE'.
  lw_fieldcat-seltext_l = 'FLDATE'.
  APPEND lw_fieldcat TO pi_fieldcat.
  CLEAR lw_fieldcat.

  lw_fieldcat-fieldname = 'SEATSMAX'.
  lw_fieldcat-seltext_l = 'SEATSMAX'.
  APPEND lw_fieldcat TO pi_fieldcat.
  CLEAR lw_fieldcat.

  lw_fieldcat-fieldname = 'SEATSOCC'.
  lw_fieldcat-seltext_l = 'SEATSOCC'.
*------------------------------------------------------------------
* field do_sum means this field do total sum
  lw_fieldcat-do_sum = 'X'.
*------------------------------------------------------------------
  APPEND lw_fieldcat TO pi_fieldcat.
  CLEAR lw_fieldcat.
ENDFORM.                    "sub_form_fieldcat

*&---------------------------------------------------------------------*
*&      Form  sub_sort
*&---------------------------------------------------------------------*
*       Sorting and point out sub total
*----------------------------------------------------------------------*
*      -->PI_SORT    text
*----------------------------------------------------------------------*
FORM sub_sort CHANGING pi_sort TYPE slis_t_sortinfo_alv.
  DATA:
    lw_sort TYPE slis_sortinfo_alv.

  lw_sort-fieldname = 'CARRID'. "sort depending on which field
  lw_sort-up = 'X'. "ascending sequence
  APPEND lw_sort TO pi_sort.
  CLEAR lw_sort.

  lw_sort-fieldname = 'CONNID'. "sort depending on which field
  lw_sort-subtot = 'X'. "sub calculate depending on this field
  lw_sort-up = 'X'. "ascending sequence
  APPEND lw_sort TO pi_sort.
  CLEAR lw_sort.
ENDFORM.                    "sub_sort

3 Comments

  1. Former Member

    Can you Add Screen Shot Of Output Window.?

  2. Hi Alex,

          Thanks. your article saved my research time. Keep Posting.... (smile)

  3. Thanks, useful code.