Skip to Content
author's profile photo Former Member
Former Member

Subtotals-ALV Grid

Hi,

Could any post solution for calculating subtotals in an ALV Report both by FM'S and by Global classes(OOABAP).

Thanks,

G Sandeep

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    Posted on Feb 25, 2009 at 08:39 AM

    Hi,

    If you are using FMs there is Import parameter IT_SORT which is a table parameter.For this table pass the parameters sort field name(fieldname) based on which field you want sub total and

    parameter subtot = 'X'.Before doing subtotals you have to pass do_sum = 'X' for the field which you want subtotals.You can do totals only on numaric fields.

    If you are using classes same as above and use SET_TABLE_FOR_FIRST_DISPLAY method.

    Best Regards,

    Surendar Reddy.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 25, 2009 at 08:44 AM

    Hi,

    For FM, you have to fill in wa_fieldcat-do_sum.

    Regards.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 25, 2009 at 08:44 AM

    Hello

    parameters must be

    subtot = 'X'

    do_sum ='X'.

    check these ....

    Thank u,

    santhosh

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 25, 2009 at 09:05 AM

    Hi,

    Go through this code, execute it...

    REPORT  ZRAJ_SUBTOTAL_ALV.
    
    TYPE-POOLS: SLIS.
    
    DATA: G_REPID LIKE SY-REPID,
    GT_SORT TYPE SLIS_T_SORTINFO_ALV,
    GS_LAYOUT TYPE SLIS_LAYOUT_ALV,
    GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
    FIELDCAT_LN LIKE LINE OF GT_FIELDCAT,
    COL_POS TYPE I.
    
    DATA: BEGIN OF ITAB,
    FIELD1(5) TYPE C,
    FIELD2(5) TYPE C,
    FIELD3(5) TYPE P DECIMALS 2,
    END OF ITAB.
    
    DATA: BEGIN OF ITAB1 OCCURS 0.
            INCLUDE STRUCTURE ITAB.
    DATA: END OF ITAB1.
    
    DATA: BEGIN OF ITAB_FIELDCAT OCCURS 0.
            INCLUDE STRUCTURE ITAB.
    DATA: END OF ITAB_FIELDCAT.
    
    
    INITIALIZATION.
      G_REPID = SY-REPID.
    
    START-OF-SELECTION.
    * TEST DATA
      MOVE 'TEST1' TO ITAB1-FIELD1.
      MOVE 'TEST1' TO ITAB1-FIELD2.
      MOVE '20.00' TO ITAB1-FIELD3.
      APPEND ITAB1.
    
      MOVE 'TEST2' TO ITAB1-FIELD1.
      MOVE 'TEST2' TO ITAB1-FIELD2.
      MOVE '40.00' TO ITAB1-FIELD3.
      APPEND ITAB1.
    
      DO 50 TIMES.
        APPEND ITAB1.
      ENDDO.
    
    END-OF-SELECTION.
    
      PERFORM BUILD_CATALOG.
      PERFORM ALV_DISPLAY.
    
    *&---------------------------------------------------------------------*
    *&      Form  build_catalog
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    FORM BUILD_CATALOG.
    * DATA FIELD CATALOG
    * Explain Field Description to ALV
      DATA: FIELDCAT_IN TYPE SLIS_FIELDCAT_ALV.
    
      CLEAR FIELDCAT_IN.
      FIELDCAT_LN-FIELDNAME = 'FIELD1'.
      FIELDCAT_LN-TABNAME = 'ITAB1'.
      FIELDCAT_LN-KEY = ' '. "SUBTOTAL KEY
      FIELDCAT_LN-NO_OUT = ' '.
      FIELDCAT_LN-SELTEXT_L = 'HEAD1'.
      APPEND FIELDCAT_LN TO GT_FIELDCAT.
    
      CLEAR FIELDCAT_IN.
      FIELDCAT_LN-FIELDNAME = 'FIELD2'.
      FIELDCAT_LN-TABNAME = 'ITAB1'.
      FIELDCAT_LN-NO_OUT = 'X'.
      FIELDCAT_LN-SELTEXT_L = 'HEAD2'.
      APPEND FIELDCAT_LN TO GT_FIELDCAT.
    
      CLEAR FIELDCAT_IN.
      FIELDCAT_LN-FIELDNAME = 'FIELD3'.
      FIELDCAT_LN-TABNAME = 'ITAB1'.
      FIELDCAT_LN-NO_OUT = ' '.
      FIELDCAT_LN-DO_SUM = 'X'. "SUM UPON DISPLAY
      APPEND FIELDCAT_LN TO GT_FIELDCAT.
    
    * DATA SORTING AND SUBTOTAL
      DATA: GS_SORT TYPE SLIS_SORTINFO_ALV.
    
      CLEAR GS_SORT.
      GS_SORT-FIELDNAME = 'FIELD1'.
      GS_SORT-SPOS = 1.
      GS_SORT-UP = 'X'.
      GS_SORT-SUBTOT = 'X'. "important statement
      APPEND GS_SORT TO GT_SORT.
    
      CLEAR GS_SORT.
      GS_SORT-FIELDNAME = 'FIELD2'.
      GS_SORT-SPOS = 2.
      GS_SORT-UP = 'X'.
    *GS_SORT-SUBTOT = 'X'. "plz note: this should be commented
      APPEND GS_SORT TO GT_SORT.
    
    ENDFORM.                    "build_catalog
    
    *&---------------------------------------------------------------------*
    *&      Form  alv_display
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    FORM ALV_DISPLAY.
      CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
      EXPORTING
    
      I_CALLBACK_PROGRAM = G_REPID
    
      I_STRUCTURE_NAME = 'ITAB1'
      IS_LAYOUT = GS_LAYOUT
      IT_FIELDCAT = GT_FIELDCAT[]
    
      IT_SORT = GT_SORT[]
    
      TABLES
      T_OUTTAB = ITAB1
      EXCEPTIONS
      PROGRAM_ERROR = 1
      OTHERS = 2.
    ENDFORM.                    "alv_display

    Note the key statements.

    Regards.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 25, 2009 at 09:11 AM

    Hi,

    Refer this wiki code:-

    https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/learn%252bto%252bdisplay%252bdata%252bin%252balv%252bgrid

    Hope this helps you.

    Thanks & Regards,

    Tarun Gambhir

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 25, 2009 at 09:20 AM

    Hi Sandeep,

    u hv a good example here..

    ALV GRID update internal table

    
    
    data:
    begin of t_itab occurs 0.
         include structure sflight.
    data end of t_itab.
    
    **declaring the container & grid for output table display**
    data:
    r_container type ref to cl_gui_custom_container,
    r_grid type ref to cl_gui_alv_grid.
    
    **structure of layout**
    data:
      fs_lay type lvc_s_layo.
    
    **Field Catalog**
    data:
     t_fcat type lvc_t_fcat,
     fs_fcat type lvc_s_fcat.
    
    **Sort table**
    data: t_sort type lvc_t_sort,
          fs_sort type lvc_s_sort.
    
    select * from sflight into corresponding fields of table t_itab.
    
    
    
      fs_fcat-fieldname = 'PRICE'.
      fs_fcat-do_sum = 'X'.
      append fs_fcat to t_fcat.
    
      fs_sort-spos = 1.
      fs_sort-fieldname = 'CONNID'.
      fs_sort-up = 'X'.
      fs_sort-subtot = 'X'.
      append fs_sort to t_sort.
    
    *--------------------------------------------------------------------------------
    call screen 100.
    *&---------------------------------------------------------------------*
    *&      Module  STATUS_0100  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    module status_0100 output.
      set pf-status 'SCREEN1'.
    
    endmodule.                 " STATUS_0100  OUTPUT
    *&---------------------------------------------------------------------*
    *&      Module  USER_COMMAND_0100  INPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    module user_command_0100 input.
      case sy-ucomm.
        when 'BACK'.
          leave to screen 0.
        when 'EXIT'.
        leave program.
      endcase.
    endmodule.                 " USER_COMMAND_0100  INPUT
    *&---------------------------------------------------------------------*
    *&      Module  LIST  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    module list output.
      create object r_container
        exporting
          container_name = 'CCONTAINER2'.
    
      create object r_grid
        exporting
          i_parent = r_container.
    *passing the layout structure, fieldcatalog and output table for display*
      call method r_grid->set_table_for_first_display
        exporting
          i_structure_name = 'SFLIGHT'
          IS_LAYOUT = fs_lay
        changing
    
          it_fieldcatalog  = t_fcat
          it_sort          = t_sort
          it_outtab        = t_itab[].
    
    
    
    endmodule.                 " LIST  OUT
    

    Regards,

    Mdi.Deeba

    Edited by: Mdi.Deeba on Feb 25, 2009 11:06 AM

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.