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

ALV SUB TOTAL

HI

AM DOING A ALV report in se 38 in which i need to display the subtotals on plant wise ,storage location wise apart from the grand total..please help me

with regards

vijay

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Mar 02, 2007 at 10:59 AM

    Check out this sample program for subtotals

    REPORT z_alv_sub_totals .
    
    TYPE-POOLS: slis.
    
    DATA: BEGIN OF it_output OCCURS 0,
              var1(8) TYPE n,
              var2(10),
              var3 TYPE I,
          END OF it_output.
    
    DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
           t_fieldcat TYPE slis_fieldcat_alv,
          it_sort TYPE slis_t_sortinfo_alv,
          t_sort TYPE slis_sortinfo_alv,
          v_repid LIKE sy-repid.
    
    INITIALIZATION.
      v_repid = sy-repid.
    
    START-OF-SELECTION.
    
      PERFORM get_data.
      PERFORM sort_fields.
      PERFORM fill_fieldcat.
      PERFORM list_display.
    
    *&---------------------------------------------------------------------*
    *&      Form  GET_DATA
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM get_data.
    
      it_output-var1 = 1000.
      it_output-var2 = 'anupama'.
      it_output-var3 = '10000'.
      APPEND it_output.
      CLEAR it_output.
    
      it_output-var1 = 1000.
      it_output-var2 = 'siddhu'.
      it_output-var3 = '20000'.
      APPEND it_output.
      CLEAR it_output.
    
      it_output-var1 = 1000.
      it_output-var2 = 'chinni'.
      it_output-var3 = '100000'.
      APPEND it_output.
      CLEAR it_output.
    
      it_output-var1 = 2000.
      it_output-var2 = 'chicchu'.
      it_output-var3 = '10000'.
      APPEND it_output.
      CLEAR it_output.
    
      it_output-var1 = 2000.
      it_output-var2 = 'candy'.
      it_output-var3 = '10000'.
      APPEND it_output.
      CLEAR it_output.
    
      it_output-var1 = 1000.
      it_output-var2 = 'anupama'.
      it_output-var3 = '10000'.
      APPEND it_output.
      CLEAR it_output.
    
      it_output-var1 = 4000.
      it_output-var2 = 'anupama'.
      it_output-var3 = '10000'.
      APPEND it_output.
      CLEAR it_output.
    
    
    ENDFORM.                    " GET_DATA
    
    *&---------------------------------------------------------------------*
    *&      Form  fill_fieldcat
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM fill_fieldcat.
      PERFORM fill_fields USING: 'IT_OUTPUT' 'VAR1' 'Variable 1' ' ',
                                 'IT_OUTPUT' 'VAR2' 'Variable 2' ' ',
                                 'IT_OUTPUT' 'VAR3' 'Variable 3' 'X'.
    
    ENDFORM.                    " fill_fieldcat
    
    *&---------------------------------------------------------------------*
    *&      Form  fill_fields
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_0146   text
    *      -->P_0147   text
    *      -->P_0148   text
    *      -->P_0149   text
    *----------------------------------------------------------------------*
    FORM fill_fields USING    value(tabname) TYPE slis_tabname
                              value(fieldname) TYPE slis_fieldname
                              value(seltext_m) LIKE dd03p-scrtext_m
                              value(do_sum) TYPE c.
      t_fieldcat-tabname = tabname.
      t_fieldcat-fieldname = fieldname.
      t_fieldcat-seltext_m  = seltext_m.
      IF do_sum = 'X'.
        t_fieldcat-datatype = 'CURR'.
      ENDIF.
      t_fieldcat-do_sum = do_sum.
      APPEND t_fieldcat TO it_fieldcat.
      CLEAR t_fieldcat.
    ENDFORM.                    " fill_fields
    
    *&---------------------------------------------------------------------*
    *&      Form  list_display
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM list_display.
      CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
       EXPORTING
         i_callback_program             = v_repid
         it_fieldcat                    = it_fieldcat
         it_sort                        = it_sort[]
       TABLES
          t_outtab                       = it_output
       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.                    " list_display
    
    *&---------------------------------------------------------------------*
    *&      Form  sort_fields
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM sort_fields.
      t_sort-fieldname = 'VAR1'.
      t_sort-tabname = 'IT_OUTPUT'.
      t_sort-spos = 1.
      t_sort-up = 'X'.
      t_sort-subtot = 'X'.
      APPEND t_sort TO it_sort.
      CLEAR t_sort.
    
      t_sort-fieldname = 'VAR3'.
      t_sort-tabname = 'IT_OUTPUT'.
      t_sort-spos = 2.
      t_sort-up = 'X'.
      APPEND t_sort TO it_sort.
      CLEAR t_sort.
    
    ENDFORM.                    " sort_fields

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 02, 2007 at 11:05 AM

    Hello,

    Check this sample report:

    REPORT ZALV. 
     
    TYPE-POOLS: SLIS. 
     
    DATA: G_REPID LIKE SY-REPID, 
    GS_PRINT            TYPE SLIS_PRINT_ALV, 
    GT_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER, 
    GT_EVENTS           TYPE SLIS_T_EVENT, 
    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. 
     
    * Print Parameters 
    PARAMETERS: 
                P_PRINT  AS CHECKBOX DEFAULT ' ', "PRINT IMMEDIATE 
                P_NOSINF AS CHECKBOX DEFAULT 'X', "NO SELECTION INFO 
                P_NOCOVE AS CHECKBOX DEFAULT ' ', "NO COVER PAGE 
                P_NONEWP AS CHECKBOX DEFAULT ' ', "NO NEW PAGE 
                P_NOLINF AS CHECKBOX DEFAULT 'X', "NO PRINT LIST INFO 
                P_RESERV TYPE I.                  "NO OF FOOTER LINE 
     
    INITIALIZATION. 
    G_REPID = SY-REPID. 
    PERFORM PRINT_BUILD    USING GS_PRINT.      "Print PARAMETERS 
     
    START-OF-SELECTION. 
    * TEST DATA 
    MOVE 'TEST1' TO ITAB1-FIELD1. 
    MOVE 'TEST1' TO ITAB1-FIELD2. 
    MOVE '10.00' TO ITAB1-FIELD3. 
    APPEND ITAB1. 
     
    MOVE 'TEST2' TO ITAB1-FIELD1. 
    MOVE 'TEST2' TO ITAB1-FIELD2. 
    MOVE '20.00' TO ITAB1-FIELD3. 
    APPEND ITAB1. 
     
    DO 50 TIMES. 
      APPEND ITAB1. 
    ENDDO. 
     
    END-OF-SELECTION. 
     
    PERFORM BUILD. 
    PERFORM EVENTTAB_BUILD CHANGING GT_EVENTS. 
    PERFORM COMMENT_BUILD  CHANGING GT_LIST_TOP_OF_PAGE. 
    PERFORM CALL_ALV. 
     
    FORM BUILD. 
    * 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-NO_OUT    = 'X'.  "FIELD NOT DISPLAY, CHOOSE FROM LAYOUT 
    <b>FIELDCAT_LN-KEY       = ' '.   "SUBTOTAL KEY</b> 
    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-REF_FIELDNAME = 'MENGE'. "<- REF FIELD IN THE DICTIONNARY 
    FIELDCAT_LN-REF_TABNAME   = 'MSEG'.  "<- REF TABLE IN THE DICTIONNARY 
    FIELDCAT_LN-NO_OUT        = ' '. 
    FIELDCAT_LN-DO_SUM        = 'X'.   "SUM UPON DISPLAY 
    APPEND FIELDCAT_LN TO GT_FIELDCAT. 
     
    <b>* DATA SORTING AND SUBTOTAL</b> 
    DATA: GS_SORT TYPE SLIS_SORTINFO_ALV. 
     
    <b>CLEAR GS_SORT. 
    GS_SORT-FIELDNAME = 'FIELD1'. 
    GS_SORT-SPOS      = 1. 
    GS_SORT-UP        = 'X'. 
    GS_SORT-SUBTOT    = 'X'. 
    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'. 
    APPEND GS_SORT TO GT_SORT.</b> 
     
    ENDFORM. 
     
    FORM CALL_ALV. 
    * ABAP List Viewer 
    CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' 
    EXPORTING 
    * I_INTERFACE_CHECK = ' ' 
    * I_BYPASSING_BUFFER = 
    * I_BUFFER_ACTIVE = ' ' 
    I_CALLBACK_PROGRAM = G_REPID 
    * I_CALLBACK_PF_STATUS_SET = ' ' 
    * I_CALLBACK_USER_COMMAND = ' ' 
    I_STRUCTURE_NAME = 'ITAB1' 
    IS_LAYOUT =  GS_LAYOUT 
    IT_FIELDCAT = GT_FIELDCAT[] 
    * IT_EXCLUDING = 
    * IT_SPECIAL_GROUPS = 
      IT_SORT = GT_SORT[] 
    * IT_FILTER = 
    * IS_SEL_HIDE = 
    * I_DEFAULT = 'X' 
    * I_SAVE = ' ' 
    * IS_VARIANT = 
      IT_EVENTS = GT_EVENTS[] 
    * IT_EVENT_EXIT = 
      IS_PRINT = GS_PRINT 
    * IS_REPREP_ID = 
    * I_SCREEN_START_COLUMN = 0 
    * I_SCREEN_START_LINE = 0 
    * I_SCREEN_END_COLUMN = 0 
    * I_SCREEN_END_LINE = 0 
    * IMPORTING 
    * E_EXIT_CAUSED_BY_CALLER = 
    * ES_EXIT_CAUSED_BY_USER = 
    TABLES 
    T_OUTTAB = ITAB1 
    EXCEPTIONS 
    PROGRAM_ERROR = 1 
    OTHERS = 2. 
    ENDFORM. 
     
    * HEADER FORM 
    FORM EVENTTAB_BUILD CHANGING LT_EVENTS TYPE SLIS_T_EVENT. 
    CONSTANTS: 
    GC_FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE'. 
    *GC_FORMNAME_END_OF_PAGE TYPE SLIS_FORMNAME VALUE 'END_OF_PAGE'. 
     
      DATA: LS_EVENT TYPE SLIS_ALV_EVENT. 
     
      CALL FUNCTION 'REUSE_ALV_EVENTS_GET' 
           EXPORTING 
                I_LIST_TYPE = 0 
           IMPORTING 
                ET_EVENTS   = LT_EVENTS. 
     
      READ TABLE LT_EVENTS WITH KEY NAME =  SLIS_EV_TOP_OF_PAGE 
                               INTO LS_EVENT. 
      IF SY-SUBRC = 0. 
        MOVE GC_FORMNAME_TOP_OF_PAGE TO LS_EVENT-FORM. 
        APPEND LS_EVENT TO LT_EVENTS. 
      ENDIF. 
     
    * define END_OF_PAGE event 
    * READ TABLE LT_EVENTS WITH KEY NAME =  SLIS_EV_END_OF_PAGE 
    *                          INTO LS_EVENT. 
    * IF SY-SUBRC = 0. 
    *   MOVE GC_FORMNAME_END_OF_PAGE TO LS_EVENT-FORM. 
    *   APPEND LS_EVENT TO LT_EVENTS. 
    * ENDIF. 
    ENDFORM. 
     
    FORM COMMENT_BUILD CHANGING GT_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER. 
      DATA: GS_LINE TYPE SLIS_LISTHEADER. 
     
      CLEAR GS_LINE. 
      GS_LINE-TYP  = 'H'. 
      GS_LINE-INFO = 'HEADER 1'. 
      APPEND GS_LINE TO GT_TOP_OF_PAGE. 
     
      CLEAR GS_LINE. 
      GS_LINE-TYP  = 'S'. 
      GS_LINE-KEY  = 'STATUS 1'. 
      GS_LINE-INFO = 'INFO 1'. 
      APPEND GS_LINE TO GT_TOP_OF_PAGE. 
      GS_LINE-KEY  = 'STATUS 2'. 
      GS_LINE-INFO = 'INFO 2'. 
      APPEND GS_LINE TO GT_TOP_OF_PAGE. 
     
    * CLEAR GS_LINE. 
    * GS_LINE-TYP  = 'A'. 
    * 
    * GS_LINE-INFO = 'ACTION'. 
    * APPEND GS_LINE TO  GT_TOP_OF_PAGE. 
     
    ENDFORM. 
     
    FORM TOP_OF_PAGE. 
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' 
           EXPORTING 
                IT_LIST_COMMENTARY = GT_LIST_TOP_OF_PAGE. 
      WRITE: SY-DATUM, 'Page No', SY-PAGNO LEFT-JUSTIFIED. 
    ENDFORM. 
     
    FORM END_OF_PAGE. 
      WRITE at (sy-linsz) sy-pagno CENTERED. 
    ENDFORM. 
     
    * PRINT SETTINGS 
    FORM PRINT_BUILD USING LS_PRINT TYPE SLIS_PRINT_ALV. 
      LS_PRINT-PRINT              = P_PRINT.  "PRINT IMMEDIATE 
      LS_PRINT-NO_PRINT_SELINFOS  = P_NOSINF. "NO SELECTION INFO 
      LS_PRINT-NO_COVERPAGE       = P_NOCOVE. "NO COVER PAGE 
      LS_PRINT-NO_NEW_PAGE        = P_NONEWP. 
      LS_PRINT-NO_PRINT_LISTINFOS = P_NOLINF. "NO PRINT LIST INFO 
      LS_PRINT-RESERVE_LINES      = P_RESERV. 
    ENDFORM.
    

    If useful reward.

    Vasanth

    Add a comment
    10|10000 characters needed characters exceeded

    • See the code changes in bold,

      DATA:X TYPE I,Y TYPE I,a type i,b type i.
      
      
      TABLES:VBRP.
      TYPE-POOLS:SLIS.
      TYPES:BEGIN OF T_VBRP,
      VBELN TYPE VBRP-VBELN,
      FBUDA TYPE VBRP-FBUDA,
      WERKS TYPE VBRP-WERKS,
      LGORT TYPE VBRP-LGORT,
      NETWR TYPE VBRP-NETWR,
      Y TYPE I,
      END OF T_VBRP.
      
      
      DATA:IT_VBRP TYPE STANDARD TABLE OF T_VBRP INITIAL SIZE 0 WITH HEADER
      LINE ,
      WA_VBRP TYPE T_VBRP.
      
      SELECT-OPTIONS DATE FOR VBRP-FBUDA OBLIGATORY.
      SELECT-OPTIONS:PLANT FOR VBRP-WERKS NO INTERVALS.
      data: fieldcatalog type slis_t_fieldcat_alv with header line,
      gd_tab_group type slis_t_sp_group_alv,
      gd_layout type slis_layout_alv,
      gd_repid like sy-repid.

      <b> DATA: wa_sortinfo TYPE slis_sortinfo_alv,

      i_sortcat TYPE slis_t_sortinfo_alv.</b>

      perform data_retrieval.
      perform build_fieldcatalog.
      perform build_layout.
       PERFORM sortcat_init CHANGING i_sortcat.
      
      perform display_alv_report.
      
      FORM build_fieldcatalog.
      
      fieldcatalog-FIELDNAME = 'FBUDA'.
      fieldcatalog-seltext_m = 'DATE '.
      fieldcatalog-col_pos = 0.
      fieldcatalog-tabname = 'it_vbrp'.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR fieldcatalog.
      
      fieldcatalog-FIELDNAME = 'VBELN'.
      fieldcatalog-seltext_m = 'BILLING DOCUMENT NUMBER '.
      fieldcatalog-col_pos = 1.
      fieldcatalog-tabname = 'it_vbrp'.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR fieldcatalog.
      
      fieldcatalog-FIELDNAME = 'WERKS'.
      fieldcatalog-seltext_m = 'PLANT '.
      fieldcatalog-col_pos = 2.
      fieldcatalog-tabname = 'it_vbrp'.

      <b>FIELDCATALOG-DO_SUM = 'X'.</b>

      APPEND fieldcatalog TO fieldcatalog.
      CLEAR fieldcatalog.
      
      fieldcatalog-FIELDNAME = 'LGORT'.
      fieldcatalog-seltext_m = 'STORAGE LOCATION '.
      fieldcatalog-col_pos = 3.
      fieldcatalog-tabname = 'it_vbrp'.

      <b>FIELDCATALOG-DO_SUM = 'X'.</b>

      APPEND fieldcatalog TO fieldcatalog.
      CLEAR fieldcatalog.
      
      fieldcatalog-FIELDNAME = 'NETWR'.
      fieldcatalog-seltext_m = 'NET VALUE'.
      fieldcatalog-col_pos = 4.
      FIELDCATALOG-DO_SUM = 'X'.
      
      fieldcatalog-tabname = 'it_vbrp'.
      APPEND fieldcatalog TO fieldcatalog.
      CLEAR fieldcatalog.
      ENDFORM.
      
      FORM build_layout.
      gd_layout-NO_INPUT = 'X'.
      gd_layout-colwidth_optimize = 'X'.
      GD_LAYOUT-TOTALS_TEXT = 'TOTALS'(201).
      ENDFORM.
      form display_alv_report.
      gd_repid = sy-repid.
      call function 'REUSE_ALV_GRID_DISPLAY'
      exporting
      i_callback_program = gd_repid
      is_layout = gd_layout
      it_fieldcat = fieldcatalog[]

      <b>it_sort = i_sortcat</b>

      i_save = 'X'
      tables
      t_outtab = IT_VBRP
      exceptions
      program_error = 1
      others = 2.
      
      if sy-subrc <> 0.
      endif.
      endform. " DISPLAY_ALV_REPORT

      <b>FORM sortcat_init CHANGING i_sortcat TYPE slis_t_sortinfo_alv.

      CLEAR wa_sortinfo.

      wa_sortinfo-fieldname = 'WERKS'.

      wa_sortinfo-tabname = 'IT_VBRP'.

      wa_sortinfo-spos = 1. " First sort by this field.

      wa_sortinfo-up = 'X'. " Ascending

      wa_sortinfo-subtot = 'X'. " Subtotal at Name1

      APPEND wa_sortinfo TO i_sortcat.

      CLEAR wa_sortinfo.

      wa_sortinfo-fieldname = 'LGORT'.

      wa_sortinfo-tabname = 'IT_VBRP'.

      wa_sortinfo-spos = 2. " Sec sort by this field.

      wa_sortinfo-up = 'X'. " Ascending

      wa_sortinfo-subtot = 'X'. " Subtotal at Name1

      APPEND wa_sortinfo TO i_sortcat.

      ENDFORM. " sortcat_init</b>

      form data_retrieval.
      
      select FBUDA VBELN WERKS LGORT NETWR
      from vbrp
      into CORRESPONDING FIELDS OF TABLE IT_VBRP
      WHERE FBUDA in date and
      WERKS IN PLANT.
      
      X = IT_VBRP-NETWR .
      ADD X TO Y.
      endform.

      Reward if this works fine.

      It will sort both on WERKS and LGORT.

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.