Skip to Content

This document stems from the document Dynamic Internal Table iIlustrated with an example of creating the transpose of internal table which explains use of field symbols, data references and creating a dynamic internal table as a transpose of an internal table using cl_alv_table_create=>create_dynamic_table.

Exploring into the much recommended RTTS method, I have found that not only is it more powerful but that it is more simpler than the old method. I will be using the same example as mentioned in the previous document to explain the use of RTTS.

As already mentioned, creation and population of dynamic internal table can be done in three steps.

  1. Creating structure
  2. Creating dynamic internal table from this structure.
  3. Populating the table.

While using the old technique (cl_alv_table_create=>create_dynamic_table,), the first step was fulfilled by creating a field catalog  and the second step using the above mentioned method call.

When creating dynamic table using RTTS, the first and second steps can be done using the methods provided by RTTS.

Before going into the details, I shall brief you very shortly on RTTS and its methods.

In ABAP Objects, there is a class-based concept called Run Time Type Information (RTTI) that you can use to determine type attributes at run time. The methods used in this class replaces statements like describe table, describe field etc.

Another concept Run time Type Creation (RTTC) was added to the above to create data of any type dynamically.

Both these together comprise the RTTS (Run time Type Services).

The RTTS contains a hierarchy of classes having attributes and methods that are used to perform these dynamic functions, which could be to get the attributes of a data object at run time or to create a data object dynamically.

Lets see our scenario again.

We have an internal table IT_DEMO containing three columns – vendor name (vend), Month(month), Amount Due(amt).


TYPES : BEGIN OF gfirst_typ,
vend(
6TYPE c,
month(
5) TYPE c,
amt     
TYPE  i.
TYPES : END OF gfirst_typ.

* Input table declarations
DATA :     it_zdemo TYPE TABLE OF gfirst_typ,
wa_zdemo
LIKE LINE OF it_zdemo.


VENDOR

MONTH

AMOUNT DUE

V100

Jan

100

V100

Feb

250

V200

Feb

216

V300

Feb

550

V200

Mar

200

V300

Mar

310

V100

Apr

145

V100

May

350

V200

May

600

V300

May

200

V400

May

800

We need to create something like a transpose for this table dynamically. The output should look like this.

VENDOR

JAN13

FEB13

MAR13

APR13

MAY13

V100

100

250

145

350

V200

216

200

600

V300

550

310

200

V400

800


Step 1 – Create Structure.

The RTTI contains attributes and methods that are used in this hierarchy of classes to get the property of variables whether it is a field or structure or table or an object instance of a class, at run time.

All these methods and attributes belong to the super class cl_abap_typedescr.

For our purpose, we shall be using the method describe_by_data to get the data type attribute of a field in the input table.

We shall be calling this method from the class cl_abap_datadescr, since the destination variable is an object type ref to cl_abap_datadescr. As we are calling the method of parent class from inherited class, we should use the casting operator ‘?=’ , rather than the simple assignment operator ‘=’, to avoid any type cast error.

ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-vend ).

Here the data type attributes of the field wa_zdemo-vend which is declared as a six character variable will be passed to ls_component-type which is data reference pointing to an object of type cl_abap_datadescr. This is the first column of the dynamic table.

DATA :

ls_component    TYPE cl_abap_structdescr=>component,

gt_component    TYPE cl_abap_structdescr=>component_table.

ls_component-name = ‘VEND’.
ls_component-
type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-vend ).
APPEND ls_component TO gt_component.

The rest of the columns are fields Jan, Feb containing the amount for that month depending on the data of the input table. (Refer the input table example shown above to understand better)

*Loop through the internal table creating a column for every distinct month in the internal table
LOOP AT it_zdemo INTO wa_zdemo.
* Search the component table if the month column already exists.
READ TABLE gt_component INTO ls_component WITH KEY name = wa_zdemo-month.
IF sy-subrc NE 0.
* The name of the column would be the month and the data type would be same as the amount field of internal table.    
ls_component-name = wa_zdemo-month.
ls_component-
type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-amt ).
APPEND ls_component TO gt_component.
ENDIF.
CLEAR : ls_component, wa_zdemo.
ENDLOOP.

Now we have the components of the structure and the table. To create the structure we shall be using the method in RTTC.


Methods in RTTC were added to the RTTS type classes to facilitate the creation of types at runtime.



To create the structure, we shall be using the static method create() in the class cl_abap_structdescr.

DATA :   gr_struct_typ   TYPE REF TO  cl_abap_datadescr.

gr_struct_typ  ?= cl_abap_structdescr=>create( p_components = gt_component ).

Step 2 – Create Table.


To create a table, first we create the table type from the structure created above. For this, we use the static method create() of the cl_abap_tabledescr.


DATA :   gr_dyntable_typ TYPE REF TO  cl_abap_tabledescr.

gr_dyntable_typ = cl_abap_tabledescr=>create( p_line_type = gr_struct_typ ).


Once the table type is created, we instantiate a reference variable to point to data object of this table type.


** Dynamic Table Declarations
DATA :  gt_dyn_table     TYPE REF TO data,
gw_dyn_line     
TYPE REF TO data,


CREATE DATA gt_dyn_table TYPE HANDLE gr_dyntable_typ.


Similarly a reference variable of the structure is also created from the structure created as a work area of the table.


CREATE DATA  gw_dyn_line TYPE HANDLE gr_struct_typ.


Step 3 – Populate internal table.

Now our dynamic table is alive and we just need to populate the data. This is done the same way it was done in the earlier document Dynamic Internal Table iIlustrated with an example of creating the transpose of internal table. You access each cell of the table using field symbols. Refer the above document for more information on field symbols and data references.


Populate the dynamic table
LOOP AT it_zdemo INTO wa_zdemo.
* Avoid duplicate entries for key field Vendor.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY (‘VEND’) = wa_zdemo-vend.
IF sy-subrc = 0.
*if <gfs_line1> is ASSIGNED.
*  UNASSIGN <gfs_line1>.
CONTINUE.
ENDIF.

ASSIGN COMPONENT ‘VEND’ OF STRUCTURE <gfs_line> TO <fs1>.
*if <fs1> is ASSIGNED.
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.
**endif.

LOOP AT gt_component INTO ls_component.
IF ls_component-name = ‘VEND’.
CONTINUE.
ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = ls_component-name INTO wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT ls_component-name OF STRUCTURE <gfs_line> TO <fs1>.
IF <fs1> IS ASSIGNED.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
ENDIF.
CLEAR : wa_zdemo1.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
CLEAR: wa_zdemo, wa_zdemo1.
ENDLOOP.



Display the contents of dynamic table in ALV


The contents of the dynamic internal table can be displayed using the factory method in cl_salv_table.

TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table   = lo_salv_table
CHANGING
t_table        = <gfs_dyn_table>
).
CATCH cx_salv_msg .
ENDTRY.

lo_salv_table->display( ).


Complete Code

*&———————————————————————*
*& Report  ZDYNAMIC_TABLE
*&———————————————————————*

REPORT  zdynamic_table_rtts.
*Author : Susmitha Susan Thomas

TYPE-POOLS : slis.
TYPES : BEGIN OF gfirst_typ,
vend(
6TYPE c,
month(
5) TYPE c,
amt     
TYPE  i.
TYPES : END OF gfirst_typ.

* Input table declarations
DATA :     it_zdemo TYPE TABLE OF gfirst_typ,
wa_zdemo
LIKE LINE OF it_zdemo,
wa_zdemo1
LIKE LINE OF it_zdemo.

** Dynamic Table Declarations
DATA :  gt_dyn_table     TYPE REF TO data,
gw_dyn_line     
TYPE REF TO data,
gw_dyn_line1    
TYPE REF TO data.

* Field Symbold Declaration
FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table>
TYPE STANDARD TABLE,
<fs1>.

* RTTS Declaratoins.
DATA :   gr_struct_typ   TYPE REF TO  cl_abap_datadescr,
gr_dyntable_typ
TYPE REF TO  cl_abap_tabledescr,
ls_component   
TYPE cl_abap_structdescr=>component,
gt_component   
TYPE         cl_abap_structdescr=>component_table.

* SALV Declarations.
DATA  : lo_cols TYPE REF TO cl_salv_columns,
lo_salv_table         
TYPE REF TO cl_salv_table,
lo_column
TYPE REF TO cl_salv_column,
col_name(
30),
col_desc(
20).

*START-OF-SELECTION.
* Populate the initial input table. Usually this input table contents will be populated at run time, which raises the requirement of dynamic table. The table contents are filled here for illustration purpose.
perform fill_table using :
‘V100’ ‘JAN’ ‘100’,
‘V100’ ‘FEB’ ‘250’,
‘V200’ ‘FEB’ ‘200’,
‘V300’ ‘FEB’ ‘150’,
‘V200’ ‘MAR’ ‘250’,
‘V300’ ‘MAR’ ‘300’,
‘V100’ ‘APR’ ‘200’,
‘V100’ ‘MAY’ ‘100’,
‘V200’ ‘MAY’ ’50’,
‘V300’ ‘MAY’ ‘125’,
‘V400’ ‘MAY’ ‘475’.

WRITE : / ‘Initial Internal Table’, /.
WRITE :/(6) ‘Vendor’,(12) ‘Month’ ,  (3) ‘Amt’ .
LOOP AT it_zdemo INTO wa_zdemo.
WRITE :/ wa_zdemo-vend, wa_zdemo-month, wa_zdemo-amt.
ENDLOOP.

* Create structure of dynamic internal table – Vendor Jan13 Feb13 Mar13 ….
ls_component-name =
‘VEND’.
ls_component-
type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-vend ).
APPEND ls_component TO gt_component.

*Loop through the internal table creating a column for every distinct month in the internal table
LOOP AT it_zdemo INTO wa_zdemo.
* Search the component table if the month column already exists.
READ TABLE gt_component INTO ls_component WITH KEY name = wa_zdemo-month.
IF sy-subrc NE 0.
* The name of the column would be the month and the data type would be same as the amount field of internal table.
ls_component-name = wa_zdemo-month.
ls_component-
type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-amt ).
APPEND ls_component TO gt_component.
ENDIF.
CLEAR : ls_component, wa_zdemo.
ENDLOOP.

gr_struct_typ  ?= cl_abap_structdescr=>create( p_components = gt_component ).
gr_dyntable_typ = cl_abap_tabledescr=>create( p_line_type = gr_struct_typ ).

CREATE DATA:
gt_dyn_table
TYPE HANDLE gr_dyntable_typ,
gw_dyn_line
TYPE HANDLE gr_struct_typ,
gw_dyn_line1
TYPE HANDLE gr_struct_typ.

ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
ASSIGN gw_dyn_line->* TO <gfs_line>.
ASSIGN gw_dyn_line1->* TO <gfs_line1>.
*
** Populate the dynamic table

*
LOOP AT it_zdemo INTO wa_zdemo.

* Avoid duplicate entries for key field Vendor.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY (‘VEND’) = wa_zdemo-vend.
IF sy-subrc = 0.
*if <gfs_line1> is ASSIGNED.
*  UNASSIGN <gfs_line1>.
CONTINUE.
ENDIF.

ASSIGN COMPONENT ‘VEND’ OF STRUCTURE <gfs_line> TO <fs1>.
*if <fs1> is ASSIGNED.
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.
**endif.

LOOP AT gt_component INTO ls_component.
IF ls_component-name = ‘VEND’.
CONTINUE.
ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = ls_component-name INTO wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT ls_component-name OF STRUCTURE <gfs_line> TO <fs1>.
IF <fs1> IS ASSIGNED.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
ENDIF.
CLEAR : wa_zdemo1.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
CLEAR: wa_zdemo, wa_zdemo1.
ENDLOOP.

TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table   = lo_salv_table
CHANGING
t_table        = <gfs_dyn_table>
).
CATCH cx_salv_msg .
ENDTRY.

* get columns object
lo_cols = lo_salv_table->get_columns( ).

*…Individual Column Names
LOOP AT gt_component INTO ls_component.
TRY.
col_name = ls_component-name.
lo_column = lo_cols->get_column( col_name ).
” < <
IF col_name = ‘VEND’.
col_desc =
‘Vendor’.
ELSE.
CONCATENATE col_name ”’13’ INTO col_desc.
ENDIF.
lo_column->set_medium_text( col_desc ).
lo_column->set_output_length(
10 ).
CATCH cx_salv_not_found.                            “#EC NO_HANDLER
ENDTRY.
ENDLOOP.
* display table

lo_salv_table->display( ).

FORM fill_table
USING p_fld1 TYPE gfirst_typ-vend
p_fld2
TYPE gfirst_typ-month
p_fld3
TYPE gfirst_typ-amt.

clear wa_zdemo.
wa_zdemo-vend = p_fld1 .
wa_zdemo-month = p_fld2.
wa_zdemo-amt = p_fld3.
APPEND wa_zdemo TO it_zdemo.

ENDFORM.                    “FILL_TABLE


Output




Acknowledgements

The figures of RTTS class hierarchy were taken from the TAW study material.

To report this post you need to login first.

22 Comments

You must be Logged on to comment or reply to a post.

  1. Suhas Saha

    Hello Sushmita,

    So finally you changed to RTTC classes for handling the dynamic internal table. Nice!

    Just a remark from my side, you should write about the disadvantages of using cl_alv_table_create=>create_dynamic_table( ) as well. So that people reading your content know that it should not be used (of course if they are on the relevant ABAP release).

    BR,

    Suhas

    (0) 
  2. Eitan Rosenberg

    Hi,

    I will be the devil advocate this time…

    I see a lot of cases when people are using “Dynamic Tables” when the number of elements is known ,
    for example the number of months or weeks in a year .

    I find the use of RTTS in those cases an overkill .

    IMHO it is much easier to declare in those cases the traditional types .

    At some point I wrote document Renaming With Suffix – One of the unsung heros…

    to show how simple it is.

    See program Y_R_EITAN_TEST_06_03 in this document .

    Thanks for this document.

    Regards.

    (1) 
  3. Former Member

    Thanks a lot Sushmita, Your code helped a lot.

    But I was just wondering how can we add: ‘REUSE_ALV_GRID_DISPLAY’ to display the output?

    (0) 
    1. Former Member

      Okay got it … looped at component table and created field catalog of type slis_t_fieldcat_alv passed it along with dynamic table to the ‘REUSE_ALV_GRID_DISPLAY’ .


      Thanks a lot again for your document 🙂

      (0) 
  4. sriramula kishore

    Hi Sushmitha,

    Nice to see your second take on dynamic internal tables. I followed the previous version in one of my reports and works fine.

    After reading Suhas Saha‘s comment now i am curious on the pros and cons of both the methods.

    But nice document overall.

    Regards

    SSK

    (0) 
  5. Former Member

    Hi ,

    The above Doc is excellent !! i have gone through the document & is very useful 

    can u please guide me on below

    1).

        Special character (&** %)are coming in column headings and system is giving dump            before and  after setting p_strict = abap_false in   method Create  (EHP6)

      

    2)  .Can we edit ALV  created in above method .

    Thanks

    Ashu 🙂

    (0) 
  6. Former Member

    Hi,


    system is giving dump for special character for my code as well. Please help to resolve the issue.

    *&———————————————————————*
    *& Report  ZDYNAMIC_TABLE
    *&———————————————————————*

    REPORT  zdynamic_table_rtts.
    *Author : Susmitha Susan Thomas

    TYPE-POOLS : slis.
    TYPES : BEGIN OF ty_final_summary,
    ZZ_DESIGTEXT (6TYPE c,               “DESIGNATION
    BTEXT(
    5) TYPE c,                       “SSA
    TOT     
    TYPE  i. “TOTAL
    TYPES : END OF ty_final_summary.

    * Input table declarations
    DATA :     it_final_summary TYPE TABLE OF ty_final_summary,
    wa_it_final_summary
    LIKE LINE OF it_final_summary,
    wa_it_final_summary1
    LIKE LINE OF it_final_summary.

    ** Dynamic Table Declarations
    DATA :  gt_dyn_table     TYPE REF TO data,
    gw_dyn_line     
    TYPE REF TO data,
    gw_dyn_line1    
    TYPE REF TO data.

    * Field Symbold Declaration
    FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
    <gfs_dyn_table>
    TYPE STANDARD TABLE,
    <fs1>.

    * RTTS Declaratoins.
    DATA :   gr_struct_typ   TYPE REF TO  cl_abap_datadescr,
    gr_dyntable_typ
    TYPE REF TO  cl_abap_tabledescr,
    ls_component   
    TYPE cl_abap_structdescr=>component,
    gt_component   
    TYPE cl_abap_structdescr=>component_table.

    * SALV Declarations.
    DATA  : lo_cols TYPE REF TO cl_salv_columns,
    lo_salv_table         
    TYPE REF TO cl_salv_table,
    lo_column
    TYPE REF TO cl_salv_column,
    col_name(
    30),
    col_desc(
    20).

    *START-OF-SELECTION.
    * Populate the initial input table. Usually this input table contents will be populated at run time, which raises the requirement of dynamic table. The table contents are filled here for illustration purpose.
    perform fill_table using :

    ‘SDE’ ‘AGR’ ‘100’,
    ‘JTO’ ‘AGR’ ‘250’,
    ‘SDE’ ‘MTR’ ‘200’,
    ‘JTO’ ‘MTR’ ‘150’,
    ‘TTA’ ‘MTR’ ‘250’.

    WRITE : / ‘Initial Internal Table’, /.
    WRITE :/(6) ‘DESIGNATION’,(12) ‘SSA’ ,  (3) ‘TOT’ .
    LOOP AT it_final_summary INTO wa_it_final_summary.
    WRITE :/ wa_ it_final_summary -DESIGNATION, wa_ it_final_summary -SSA, wa_ it_final_summary -TOT.
    ENDLOOP.

    * Create structure of dynamic internal table – DESIGNATION SSA1 SSA2…

    ls_component-name = ZZ_DESIGTEXT.
    ls_component-
    type ?= cl_abap_datadescr=>describe_by_data( wa_final_summary- ZZ_DESIGTEXT ).
    APPEND ls_component TO gt_component.

    *Loop through the internal table creating a column for every distinct SSA in the internal table
    LOOP AT it_final_summary INTO wa_final_summary
    * Search the component table if the SSA column already exists.
    READ TABLE gt_component INTO ls_component WITH KEY name = wa_final_summarybtext.
    IF sy-subrc NE 0.
    * The name of the column would be the SSA and the data type would be same as the TOT field of internal table.
    ls_component-name =
    wa_final_summarybtext.
    ls_component-
    type ?= cl_abap_datadescr=>describe_by_data(wa_final_summarytot).
    APPEND ls_component TO gt_component.
    ENDIF.
    CLEAR : ls_component, wa_final_summary.
    ENDLOOP.

    gr_struct_typ  ?= cl_abap_structdescr=>create( p_components = gt_component ).
    gr_dyntable_typ = cl_abap_tabledescr=>create( p_line_type = gr_struct_typ ).

    CREATE DATA:
    gt_dyn_table
    TYPE HANDLE gr_dyntable_typ,
    gw_dyn_line
    TYPE HANDLE gr_struct_typ,
    gw_dyn_line1
    TYPE HANDLE gr_struct_typ.

    ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
    ASSIGN gw_dyn_line->* TO <gfs_line>.
    ASSIGN gw_dyn_line1->* TO <gfs_line1>.
    *
    ** Populate the dynamic table

    *

    LOOP AT it_final_summary INTO wa_final_summary


    * Avoid duplicate entries for key field DESIGNATION.
    READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY (ZZ_DESIGTEXT) = wa_final_summary-ZZ_DESIGTEXT.
    IF sy-subrc = 0.
    *if <gfs_line1> is ASSIGNED.
    *  UNASSIGN <gfs_line1>.
    CONTINUE.
    ENDIF.

    ASSIGN COMPONENT ZZ_DESIGTEXT OF STRUCTURE <gfs_line> TO <fs1>.
    *if <fs1> is ASSIGNED.
    <fs1> =
    wa_final_summary-ZZ_DESIGTEXT.
    UNASSIGN <fs1>.
    **endif.

    LOOP AT gt_component INTO ls_component.
    IF ls_component-name = ZZ_DESIGTEXT.
    CONTINUE.
    ENDIF.
    READ TABLE it_final_summary WITH KEY ZZ_DESIGTEXT = wa_final_summary-ZZ_DESIGTEXT btext = ls_component-name INTO wa_final_summary1.
    IF sy-subrc = 0.
    ASSIGN COMPONENT ls_component-name OF STRUCTURE <gfs_line> TO <fs1>.
    IF <fs1> IS ASSIGNED.
    <fs1> =
    wa_final_summary1-tot.
    UNASSIGN <fs1>.
    ENDIF.
    ENDIF.
    CLEAR : wa_zdemo1.
    ENDLOOP.
    APPEND <gfs_line> TO <gfs_dyn_table>.
    CLEAR: <gfs_line>.
    CLEAR: wa_final_summary, wa_final_summary1.
    ENDLOOP.

    TRY.
    cl_salv_table=>factory(
    IMPORTING
    r_salv_table   = lo_salv_table
    CHANGING
    t_table        = <gfs_dyn_table>
    ).
    CATCH cx_salv_msg .
    ENDTRY.

    * get columns object
    lo_cols = lo_salv_table->get_columns( ).

    *…Individual Column Names
    LOOP AT gt_component INTO ls_component.
    TRY.
    col_name = ls_component-name.
    lo_column = lo_cols->get_column( col_name ).
    ” < <
    IF col_name = ZZ_DESIGTEXT.
    col_desc =
    ‘DISGNATION’.
    ELSE.
    “CONCATENATE col_name ”’13’ INTO col_desc.
    ENDIF.
    lo_column->set_medium_text( col_desc ).
    lo_column->set_output_length(
    10 ).
    CATCH cx_salv_not_found. “#EC NO_HANDLER
    ENDTRY.
    ENDLOOP.
    * display table

    lo_salv_table->display( ).

    FORM fill_table
    USING p_fld1 TYPE gfirst_typ-vend
    p_fld2
    TYPE gfirst_typ-month
    p_fld3
    TYPE gfirst_typ-amt.

    clear wa_zdemo.
    wa_zdemo-vend = p_fld1 .
    wa_zdemo-month = p_fld2.
    wa_zdemo-amt = p_fld3.
    APPEND wa_zdemo TO it_zdemo.

    ENDFORM.       

    (0) 
  7. Former Member

    Thanks for the document. 🙂

    A small tip if you create a table by this method you can also clearly understand casting concept( This is the topic from where i understood casting ).

    (0) 
  8. Valerie Lee

    Excellent.  I have a similar requirement, but instead of only display one column per month, I need to display multiple columns per month.  How do you build it to fill the table using RTTS?  Say users want to see the year-to-date trend of Revenue sales of  vendors located in US and China.  My input would be something like this.

     Jan Vendor Number Name USA branch China branch Percentage China vs total

     

    Jan 901 A  $             80.00  $          20.00 20%
    Jan 902 B  $             50.00  $          10.00 17%
    Jan 905 C  $           120.00  $        150.00 56%
    Jan 910 D  $                    –  $        400.00 100%
    Jan 912 X  $           500.00  $                 – 0%
    Feb 901 A  $           140.00  $          60.00 30%
    Feb 902 B  $             75.00  $          25.00 25%
    Feb 905 C  $           230.00  $        150.00 39%
    Feb 910 D  $           100.00  $        400.00 80%
    Feb 912 X  $           210.00  $          90.00 30%

    and the output after tranpose the monthly column to row:

    Jan-17 Feb-17
    Vendor Number Vendor Name USA China % China vs. Total USA China % China vs. Total
    901 Company A  $             80.00  $          20.00 20%  $  140.00  $    60.00 30%
    902 Company B 50 10 17% 75 25 25%
    905 Company C 120 150 56% 230 150 39%
    910 Company D 0 400 100% 100 400 80%
    912 Company X 500 0 0% 210 90 30%

     

    (0) 
  9. Gerrit Beukema

    Great blog, I used it to answer this question on Stackoverflow. I took your solution and made it more dynamic, i.e. the method that displays the data does not have any knowledge of the table it is processing.

    Here is the code:

    REPORT zso_group_alv.
    
    
    TYPES: BEGIN OF ts_data,
             field1 TYPE char10,
             group  TYPE i,
             val    TYPE i,
           END OF ts_data,
           tt_data TYPE STANDARD TABLE OF ts_data.
    
    DATA: gt_data TYPE tt_data.
    
    CLASS lcl_alv_grouped DEFINITION.
    
      PUBLIC SECTION.
    
        CLASS-METHODS:
          show_table_grouped_by IMPORTING VALUE(it_data)   TYPE ANY TABLE
                                          i_group_by       TYPE fieldname
                                          i_aggregate_from TYPE fieldname,
          group_fld_name IMPORTING i_value         TYPE any
                         RETURNING VALUE(fld_name) TYPE fieldname.
    
    ENDCLASS.
    
    
    CLASS lcl_alv_grouped IMPLEMENTATION.
    
      METHOD group_fld_name.
        " Returns the field name for the group fields
        " This is to make sure there are no reserved names used or
        " reuse of already existing fields
        fld_name = 'fld_' && i_value.
      ENDMETHOD.
    
      METHOD show_table_grouped_by.
        " Shows the data in table IT_DATA in an ALV
        " The data is transposed
        " The data from field I_AGGREGATE_FROM is summed into groups
        " from the values in the field I_GROUP_BY
        " All the other fields in the table are treated as the key
        " fields for the transposed table
    
        DATA: ls_component      TYPE cl_abap_structdescr=>component,
              lt_components     TYPE cl_abap_structdescr=>component_table,
              lr_struct         TYPE REF TO cl_abap_structdescr,
              lt_keys           TYPE abap_sortorder_tab,
              lt_key_components TYPE cl_abap_structdescr=>component_table,
              "ls_keys           TYPE REF TO data,
              lr_trans_table    TYPE REF TO data,
              lr_trans_wa       TYPE REF TO data,
              lr_keys_type      TYPE REF TO cl_abap_structdescr,
              ls_keys           TYPE REF TO data,
              ls_keys_prev      TYPE REF TO data,
              lr_salv           TYPE REF TO cl_salv_table.
    
        FIELD-SYMBOLS: <trans_table> TYPE STANDARD TABLE.
    
    
        " Determine the fields in the transposed table
        LOOP AT it_data ASSIGNING FIELD-SYMBOL(<data>).
    
          AT FIRST.
            " Get field to aggregate
            ASSIGN COMPONENT i_aggregate_from OF STRUCTURE <data> TO FIELD-SYMBOL(<aggregate_field>).
            IF sy-subrc NE 0.
              RETURN. " Would be nice to tell the calling program about this error
            ENDIF.
    
            " Gather all the other fields from the data table, these are treated as keys
            lr_struct ?= cl_abap_datadescr=>describe_by_data( <data> ).
            LOOP AT lr_struct->get_components( ) ASSIGNING FIELD-SYMBOL(<component>).
              IF <component>-name NE i_aggregate_from AND
                 <component>-name NE i_group_by.
                APPEND <component> TO: lt_components,
                                       lt_key_components.
                APPEND VALUE #( name = <component>-name
                                descending = abap_false
                                astext = abap_true
                              ) TO lt_keys.
              ENDIF.
            ENDLOOP.
    
          ENDAT. " FIRST
    
          " Get the group by field
          ASSIGN COMPONENT i_group_by OF STRUCTURE <data> TO FIELD-SYMBOL(<group_field>).
          IF sy-subrc NE 0.
            RETURN. " Would be nice to tell the calling program about this error
          ENDIF.
    
          " Gather all the values in the group by field
          DATA(l_new_group) = group_fld_name( <group_field> ).
          READ TABLE lt_components WITH KEY name = l_new_group TRANSPORTING NO FIELDS.
          IF sy-subrc NE 0.
            ls_component-name = l_new_group.
            ls_component-type ?= cl_abap_datadescr=>describe_by_data( <aggregate_field> ).
            APPEND ls_component TO lt_components.
          ENDIF.
    
        ENDLOOP. " IT_DATA
    
        " LT_COMPONENTS is now filled with all the fields to show in the ALV
    
        " Create the transpose table and fill its
        DATA(lr_trans_table_type) = cl_abap_tabledescr=>create( cl_abap_structdescr=>create( lt_components ) ).
        CREATE DATA lr_trans_table TYPE HANDLE lr_trans_table_type.
        ASSIGN lr_trans_table->* TO <trans_table>.
    
        " Data needs to be sorted to generate the rows in the transposed table
        SORT it_data BY (lt_keys).
    
        " Create structures to keep track of the key values
        lr_keys_type ?= cl_abap_structdescr=>create( lt_key_components ).
        CREATE DATA ls_keys TYPE HANDLE lr_keys_type.
        CREATE DATA ls_keys_prev TYPE HANDLE lr_keys_type.
        ASSIGN ls_keys->*      TO FIELD-SYMBOL(<keys>).
        ASSIGN ls_keys_prev->* TO FIELD-SYMBOL(<keys_prev>).
    
        " Transpose the data
        LOOP AT it_data ASSIGNING <data>.
    
          MOVE-CORRESPONDING <data> TO <keys>.
          IF <keys> NE <keys_prev>.
            " Found a new key combination, add a row to the transposed table
            APPEND INITIAL LINE TO <trans_table> ASSIGNING FIELD-SYMBOL(<trans_data>).
            MOVE-CORRESPONDING <data> TO <trans_data>. " Filling the key fields
          ENDIF.
    
          " Agragate the value into the right group
          ASSIGN COMPONENT i_aggregate_from          OF STRUCTURE <data>       TO FIELD-SYMBOL(<value>).
          ASSIGN COMPONENT i_group_by                OF STRUCTURE <data>       TO FIELD-SYMBOL(<group>).
          ASSIGN COMPONENT group_fld_name( <group> ) OF STRUCTURE <trans_data> TO FIELD-SYMBOL(<trans_value>).
          ADD <value> TO <trans_value>.
    
          " Remember keys to compare with the next row
          <keys_prev> = <keys>.
    
        ENDLOOP. " IT_DATA
    
        " Display transposed data in ALV
        TRY.
            cl_salv_table=>factory(
    *        EXPORTING
    *          list_display   = IF_SALV_C_BOOL_SAP=>FALSE    " ALV Displayed in List Mode
    *          r_container    =     " Abstract Container for GUI Controls
    *          container_name =
              IMPORTING
                r_salv_table   = lr_salv    " Basis Class Simple ALV Tables
              CHANGING
                t_table        = <trans_table>
            ).
          CATCH cx_salv_msg.    "
            " Some error handling would be nice
    
        ENDTRY.
    
        " Will need to do something about the column headers
    
        lr_salv->display( ).
    
      ENDMETHOD.
    
    ENDCLASS.
    
    
    
    
    START-OF-SELECTION.
    
      APPEND VALUE #( field1 = '64000' group = 1 val = 10 ) TO gt_data.
      APPEND VALUE #( field1 = '64000' group = 1 val = 15 ) TO gt_data.
      APPEND VALUE #( field1 = '64010' group = 1 val = 20 ) TO gt_data.
      APPEND VALUE #( field1 = '64010' group = 2 val = 15 ) TO gt_data.
      APPEND VALUE #( field1 = '64020' group = 2 val = 10 ) TO gt_data.
      APPEND VALUE #( field1 = '64020' group = 2 val = 20 ) TO gt_data.
    
      lcl_alv_grouped=>show_table_grouped_by(
        EXPORTING
          it_data          = gt_data
          i_group_by       = 'GROUP'
          i_aggregate_from = 'VAL'
      ).
    (0) 

Leave a Reply