Skip to Content
avatar image
Former Member

subtotal on a field in OOPS alv

hi,

how can we get subtotal on a certain field in the ALV display?

thanks,

Anil.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    Jul 17, 2008 at 05:57 AM

    Hi,

    U can try this also.

    Added one extra field in the ALV output table and populated it with a constant value (This is nothing but the text that need to be displayed as Total text) for all the records.

    Hide this field in field catalog level

    Populated the sort table for ALV with this field and enable subtotal display for it.

    Hide total line display in ALV by passing appropriate value in the Layout structure.

    As this extra field contains constant value for all the records so subtotal on it is equivalent to total for rest of the fields.

    Ex:

    Output table

    Extra Field

    Field1

    Field2

    Field3

    Field4

    Total:

    A

    B

    10

    10

    Total:

    A

    A

    10

    10

    Total:

    B

    C

    10

    10

    Total:

    C

    D

    10

    10

    Final Output in ALV after subtotal on Extra Field

    Field1

    Field2

    Field3

    Field4

    A

    B

    10

    10

    A

    A

    10

    10

    B

    C

    10

    10

    C

    D

    10

    10

    Total:

    40

    40

    Code:

    &----


    *& Report Z_ALV_DEMO_TOTAL_TEXT

    *&

    &----


    *&

    *&

    &----


    REPORT z_alv_demo_total_text.* Type declaration for final table to display the output

    TYPES: BEGIN OF ty_mara,

    srno TYPE char40, " Storing the total text

    matnr TYPE matnr, " Material

    ersda TYPE ersda, " Creation date

    ernam TYPE ernam, " Created by

    laeda TYPE laeda, " Last change date

    aenam TYPE aenam, " Last change by

    vpsta TYPE vpsta, " Maintenance status

    brgew TYPE brgew, " Gross weight

    ntgew TYPE ntgew, " Net weight

    gewei TYPE gewei, " Weight Unit

    END OF ty_mara.* Type declaration for table storing temp. data

    TYPES: BEGIN OF ty_mara_tmp,

    matnr TYPE matnr, " Material

    ersda TYPE ersda, " Creation date

    ernam TYPE ernam, " Created by

    laeda TYPE laeda, " Last change date

    aenam TYPE aenam, " Last change by

    vpsta TYPE vpsta, " Maintenance status

    brgew TYPE brgew, " Gross weight

    ntgew TYPE ntgew, " Net weight

    gewei TYPE gewei, " Weight Unit

    END OF ty_mara_tmp.* Internal table for storing final data

    DATA: i_mara TYPE STANDARD TABLE OF ty_mara INITIAL SIZE 0.* Work area for final table

    DATA: w_mara TYPE ty_mara.

    • Internal table for storing temp. data

    DATA: i_mara_tmp TYPE STANDARD TABLE OF ty_mara_tmp INITIAL SIZE 0.* Work area for temp. table

    DATA: w_mara_tmp TYPE ty_mara_tmp.* Object variable for ALV grid

    DATA: oref1 TYPE REF TO cl_gui_alv_grid.* Field catalog table for ALV grid

    DATA: fieldcat TYPE lvc_t_fcat.* Workarea for field catalog table

    DATA: w_field TYPE lvc_s_fcat.* Internal table for storing info. for ALV grid

    data: i_sort2 TYPE STANDARD TABLE OF lvc_s_sort INITIAL SIZE 0.* Workarea for sort table

    DATA: wa_sort2 TYPE lvc_s_sort.* Workarea for ALV layout

    data: wa_layout TYPE lvc_s_layo.

    START-OF-SELECTION.* Fetch data

    SELECT matnr " Material

    ersda " Creation date

    ernam " Created by

    laeda " Last change date

    aenam " Last change by

    vpsta " Maintenance status

    brgew " Gross weight

    ntgew " Net weight

    gewei " Weight Unit

    FROM mara

    INTO TABLE i_mara_tmp

    UP TO 100 ROWS. CHECK sy-subrc = 0.* Populate final table

    LOOP AT i_mara_tmp INTO w_mara_tmp.* Storing the Total text need to be displayed in

    • ALV

    w_mara-srno = 'Total weight (Gross & Net)'.

    w_mara-matnr = w_mara_tmp-matnr.

    w_mara-ersda = w_mara_tmp-ersda. w_mara-ernam = w_mara_tmp-ernam . w_mara-laeda = w_mara_tmp-laeda. w_mara-aenam = w_mara_tmp-aenam.

    w_mara-vpsta = w_mara_tmp-vpsta.

    w_mara-brgew = w_mara_tmp-brgew.

    w_mara-ntgew = w_mara_tmp-ntgew.

    w_mara-gewei = w_mara_tmp-gewei.

    APPEND w_mara TO i_mara. ENDLOOP.

    • Calling the screen to display ALV

    CALL SCREEN 100.

    &----


    *& Module STATUS_0100 OUTPUT

    &----


    • Display ALV report

    ----


    MODULE status_0100 OUTPUT.

    IF oref1 IS INITIAL.* Create ALV grid object

    • In this case we have not created any custom container in the screen,

    • Instead of that dummy container name is passed

    • ADVANTAGE: we can run this report in background without any problem

    CREATE OBJECT oref1

    EXPORTING

    i_parent = cl_gui_custom_container=>screen0

    EXCEPTIONS

    error_cntl_create = 1

    error_cntl_init = 2

    error_cntl_link = 3

    error_dp_create = 4

    OTHERS = 5

    .

    CHECK sy-subrc = 0.* Preparing the field catalog

    • ZDEMO: Defined in DDIC, it's structure is same as TYPE ty_mara

    • defined in the program

    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

    EXPORTING

    i_structure_name = 'ZDEMO'

    CHANGING

    ct_fieldcat = fieldcat

    EXCEPTIONS

    inconsistent_interface = 1

    program_error = 2

    OTHERS = 3.

    IF sy-subrc = 0. LOOP AT fieldcat INTO w_field. IF w_field-fieldname = 'BRGEW' OR

    w_field-fieldname = 'NTGEW'.

    • Summation for Gross & Net weight

    w_field-do_sum = 'X'.

    MODIFY fieldcat FROM w_field TRANSPORTING do_sum. ENDIF. IF w_field-fieldname = 'SRNO'.

    • Hide this field so that it can display it's content i.e.

    • Total text in Subtotal level

    w_field-tech = 'X'.

    w_field-no_out = 'X'.

    MODIFY fieldcat FROM w_field TRANSPORTING tech no_out. ENDIF. CLEAR w_field.

    ENDLOOP.

    ENDIF.* Populate Sort table with SRNO field so that we can display the total

    • text in it's subtotal level

    wa_sort2-spos = 1.

    wa_sort2-fieldname = 'SRNO'.

    wa_sort2-up = 'X'.

    wa_sort2-subtot = 'X'.

    APPEND wa_sort2 TO i_sort2.* Hide the total line

    wa_layout-no_totline = 'X'.* Display the ALV grid

    CALL METHOD oref1->set_table_for_first_display

    EXPORTING

    is_layout = wa_layout

    CHANGING

    it_outtab = i_mara[]

    it_fieldcatalog = fieldcat

    it_sort = i_sort2

    EXCEPTIONS

    invalid_parameter_combination = 1

    program_error = 2

    too_many_lines = 3

    OTHERS = 4.

    IF sy-subrc <> 0. ENDIF.* Set the focus on the grid

    CALL METHOD cl_gui_alv_grid=>set_focus

    EXPORTING

    control = oref1

    EXCEPTIONS

    cntl_error = 1

    cntl_system_error = 2

    OTHERS = 3.

    IF sy-subrc <> 0.

    ENDIF. ENDIF.ENDMODULE. " STATUS_0100 OUTPUT

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 17, 2008 at 05:41 AM
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 17, 2008 at 05:52 AM

    Hi,

    Refer to the following link for reference code.

    http://saptechnical.com/Tutorials/ALV/Subtotals/text.htm

    Regards,

    Jaya Vani

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 17, 2008 at 05:56 AM

    HI,

    Check the do_sum field in the catalog.

    Regards,

    Priyanka

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 17, 2008 at 06:08 AM

    Hi Anilkumar,

    Do like this,

    >1. Create the object reference variable and receive the object using the GET_AGGREGATIONS method of the

    > GR_TABLE object.

    >2. Add the aggregation by calling the ADD_AGGREGATION method of the GR_SORTS object.

    >3. Also need to modify the call to ADD_SORT to set the SUBTOTAL = ABAP_TRUE.

    Example / Code snippets:

    report zalv_subtotal.

    data: ispfli type table of spfli.

    data: gr_table type ref to cl_salv_table.

    data: gr_functions type ref to cl_salv_functions.

    data: gr_display type ref to cl_salv_display_settings.

    data: gr_columns type ref to cl_salv_columns_table.

    data: gr_column type ref to cl_salv_column_table.

    data: gr_sorts type ref to cl_salv_sorts.

    data: gr_agg type ref to cl_salv_aggregations. "for aggregation

    data: color type lvc_s_colo.

    start-of-selection.

    select * into table ispfli from spfli.

    cl_salv_table=>factory( importing r_salv_table = gr_table

    changing t_table = ispfli ).

    gr_functions = gr_table->get_functions( ).

    gr_functions->set_all( abap_true ).

    gr_display = gr_table->get_display_settings( ).

    gr_display->set_striped_pattern( cl_salv_display_settings=>true ).

    gr_display->set_list_header( 'This is the heading' ).

    gr_columns = gr_table->get_columns( ).

    gr_column ?= gr_columns->get_column( 'CITYTO' ).

    gr_column->set_long_text( 'This is long text' ).

    gr_column->set_medium_text( 'This is med text' ).

    gr_column->set_short_text( 'This is sh' ).

    gr_column ?= gr_columns->get_column( 'CITYFROM' ).

    color-col = '6'.

    color-int = '1'.

    color-inv = '0'.

    gr_column->set_color( color ).

    gr_sorts = gr_table->get_sorts( ).

    gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ). "for subtotal

    gr_agg = gr_table->get_aggregations( ).

    gr_agg->add_aggregation( 'DISTANCE' ).

    gr_table->display( ).

    ***************************************

    Best Regards,

    Sayak...

    "Giving points for helpful answer is always appreciating"

    Add comment
    10|10000 characters needed characters exceeded