Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to do subtotalling when using CL_GUI_ALV_GRID Class?

Former Member
0 Kudos

I am using CL_GUI_ALV_GRID Class in a report. In this report, I have to generate a summary report where the report has to look summarized based on four fields viz. G/L Account, Profit Center, Plant, Material Group as per mentioned in the given order of heirarchy. The subtotalling has to be done on the amount field(WRBTR).

Can someone please let me know, as how can this be done? Do let me know, if you require any other information from my side.

Thanks & Regards,

Ravi

10 REPLIES 10

Former Member
0 Kudos

Hi,

go to SLIS and then search for subtotal field and make it as 'X' for the field you want to get the subtotal similarly for the total also

0 Kudos

Hi Shiva,

Thanks for the reply!!

But, I have already tried putting a 'X' by adding a fieldcatalogue of DO_SUM. But it has not worked.

Can you elaborate more on your reply?

Regards,

Ravi

Former Member
0 Kudos

check if this is helpful.

PERFORM f007_set_aggregations USING lr_aggregations

'D5_LCL'.

&----


*& Form f007_set_aggregations

&----


  • Function to calculate the subtotals

----


FORM f007_set_aggregations USING ir_aggregations TYPE REF TO cl_salv_aggregations

lw_col TYPE any.

constants: lc_1 type SALV_DE_AGGREGATION value '1'.

TRY.

ir_aggregations->set_numerical_aggregation( abap_true ).

TRY.

ir_aggregations->add_aggregation(

columnname = lw_col

aggregation = lc_1 ).

CATCH cx_salv_data_error cx_salv_existing cx_salv_not_found.

MESSAGE i023(salv_exception).

CATCH cx_salv_method_not_supported.

MESSAGE i028(salv_exception) WITH space space space space.

ENDTRY.

CATCH cx_salv_method_not_supported.

MESSAGE i028(salv_exception) WITH space space space space.

ENDTRY.

ENDFORM. "f007_set_aggregations

0 Kudos

Hi Prabhu,

Many Thanks for the prompt reply.

But what you mentioned...it makes use of some other class.

I need a method (of aggregation, summing up, or subtotal) in CL_GUI_ALV_GRID class.

but anyways...thanks !

Regards,

Ravi

0 Kudos

Hello Ravi,

Sort on the field based on which you want the sub-totals for amount to appear. For instance, if you want plant wise sub-total, add an entry in the table it_sort for field PLANT.

Regards,

Manoj

0 Kudos

Hi Manoj,

Thanks for the reply.

I have sorted the field as well. But, I need to sum it up further. and that, summing up is not happening.

Ravi

Former Member
0 Kudos

Hi,

Can someone please provide a direction?

Its kindda urgent!!

Regards,

Ravi

0 Kudos

Hi Ravi ,

Use the sort table and pass it , see the table type LVC_T_SORT , it contains the structure LVC_S_SORT which has a field SUBTOT , try using this.

Regards

Arun

0 Kudos

Hi,

  CALL METHOD o_alvgrid->set_table_for_first_display
     EXPORTING
       is_variant                    = w_variant
       i_save                        = c_a
*    I_DEFAULT                     = 'X'
       is_layout                     = p_layout
*    IS_PRINT                      =
       it_special_groups             = p_groups[]
       it_toolbar_excluding          = p_exclude[]
*    IT_HYPERLINK                  =
*    IT_ALV_GRAPHICS               =
*    IT_EXCEPT_QINFO               =
    CHANGING
       it_outtab                     = p_output[]
       it_fieldcatalog               = p_fieldcat[]
      IT_SORT                       = it_sort
*    IT_FILTER                     =
    EXCEPTIONS
       invalid_parameter_combination = 1
       program_error                 = 2
       too_many_lines                = 3
       OTHERS                        = 4.

f8000_sortcat_init CHANGING it_sort TYPE LVC_S_SORT.
  CLEAR wa_sortinfo.
  wa_sortinfo-fieldname = 'RMATNR'.
  wa_sortinfo-tabname = 'I_OUTTAB'.
  wa_sortinfo-spos = 2.   " First sort by this field.
  wa_sortinfo-up = 'X'.    "   Ascending
  wa_sortinfo-subtot = 'X'.    " Subtotal at Name1
  APPEND wa_sortinfo TO it_sort.

  CLEAR wa_sortinfo.
  wa_sortinfo-fieldname = 'CPUDT'.
  wa_sortinfo-tabname = 'I_OUTTAB'
  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 it_sort.

Please refer the structure LVC*SORT I have takedn teh code form SLIS.

If you do so you can get the subtotal.

Also make sure that you have to enable to SUM = 'X' for the particular field in fieldcatalog.

Former Member
0 Kudos

Hi Ravi,

I've used the following code to toal the no. of entries based on field MSGRP.

V_DIV = 'DEP_TOT'.

SORT ITABMEL1 BY SWERK MSGRP .

LOOP AT ITABMEL1 WHERE SWERK NE V_DIV

IDX = IDX1 + 1.

AT END OF MSGRP.

GRANDTOTAL = GRANDTOTAL + 1.

IF DIV <> ITABMEL1-MSGRP.

DIV = ITABMEL1-MSGRP.

CLEAR ITABMEL1.

ITABMEL1-DIV = DIV.

ITABMEL1-SWERK = 'DEP_TOT'.

ITABMEL1-MSGRP = COUNT.

CLEAR COUNT.

IDX1 = IDX + 1.

INSERT ITABMEL1 INDEX IDX1.

ELSE.

CLEAR ITABMEL1.

COUNT1 = COUNT + 1.

DELETE ITABMEL1 INDEX IDX1.

ITABMEL1-SWERK = 'DEP_TOT'.

ITABMEL1-MSGRP = COUNT1.

CLEAR COUNT1.

IDX2 = IDX.

INSERT ITABMEL1 INDEX IDX2.

IDX1 = IDX2.

ENDIF.

ENDAT.

COUNT = COUNT + 1.

DIV = ITABMEL1-MSGRP.

ITABMEL1-DIV = DIV.

MODIFY ITABMEL1.

ENDLOOP.

Hope this helps.

Reward if helpful.

Regards,

Sipra