cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Adding new line "TOTAL" after data group

Dio
Explorer
2,112

Hi Experts,

Name's Dio, I have a need to add a new line after the group data, that new line is the subtotal of the 'WRBTR' field. I've been looking for a way for 3 days and haven't found a way. Please help me to solved this, I appreciate any help.

Capture.JPG

Regards,
Dio

 

Accepted Solutions (1)

Accepted Solutions (1)

Uli75
Explorer
0 Kudos
cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lo_salv)
                        CHANGING t_table = lt_table ).

lo_salv->get_sorts( )->add_sort( columnname = 'LIFNR' subtotal = abap_true      sequence = if_salv_c_sort=>sort_up ).

lo_salv->get_aggregations( )->add_aggregation( columnname = 'WRBTR' aggregation = if_salv_c_aggregation=>total ).

lo_salv->display( ).

Quick solution to display the table with subtotals in an ALV...

 

Dio
Explorer
0 Kudos

Hi, 

Thank you for your answer friend, I got another way to do this. here's the code :

READ TABLE it_data INTO DATA(work_area2) INDEX 1.

  IF sy-subrc IS INITIAL.
    old_val = work_area2-znme1.
  ENDIF.

  LOOP AT it_data INTO work_area.

    IF work_area-znme1 <> old_val.
      new_line-znme1 = 'SUBTOTAL'.
      new_line-wrbtr = sum.
      INSERT new_line INTO it_data INDEX sy-tabix.
      CLEAR sum.
      old_val = work_area-znme1.
    ENDIF.

    sum = sum + work_area-wrbtr.
  ENDLOOP.

  data: v_lin type i.

  DESCRIBE TABLE it_data lines v_lin.
  READ TABLE it_data into data(work_area3) index v_lin.

  new_line-payment = 'GRAND TOTAL'.
  new_line-wrbtr = sum.
  INSERT new_line INTO it_data INDEX v_lin.

Regards,
Dio

Answers (2)

Answers (2)

 

TYPES : BEGIN OF ty_line,
              lifnr  TYPE char10,
              belnr  TYPE char10,
              amount TYPE decfloat16,
            END OF ty_line,
            ty_lines TYPE STANDARD TABLE OF ty_line WITH DEFAULT KEY.

    DATA(gt_main) = VALUE ty_lines( ( lifnr = 'AAA' belnr = '123' amount = 10 )
                                    ( lifnr = 'AAA' belnr = '234' amount = 20 )
                                    ( lifnr = 'BBB' belnr = '456' amount = 30 )
                                    ( lifnr = 'CCC' belnr = '567' amount = 40 )
                                    ( lifnr = 'CCC' belnr = '789' amount = 50 )
                                    ( lifnr = 'CCC' belnr = '901' amount = 60 ) ).

    DATA(lt_display) = VALUE ty_lines(
            ( LINES OF VALUE #(
                  FOR GROUPS <g> OF <line> IN gt_main
                  GROUP BY ( lifnr = <line>-lifnr )
                  ( LINES OF VALUE #( FOR <line2> IN GROUP <g> ( <line2> ) ) )
                  ( lifnr = 'SUBTOTAL'
                    amount = REDUCE #( INIT subtotal TYPE ty_line-amount
                                       FOR <line2> IN GROUP <g>
                                       NEXT subtotal = subtotal + <line2>-amount ) ) ) )
            ( lifnr = 'TOTAL'
              amount = REDUCE #( INIT total TYPE ty_line-amount
                                 FOR <line> IN gt_main
                                 NEXT total = total + <line>-amount ) ) ).

 

I will try to add the raw solution without the inputs on what group we are trying. I'm assuming it will be LIFNR.

Inspired from - https://stackoverflow.com/a/52768142

 

Dio
Explorer
0 Kudos
Hi Shashank,
Dio
Explorer
0 Kudos

Hi Shashank, 

Thank you for the answer, I'll try it and I'll let you know how it goes.

Regards,
Dio

Dio
Explorer
0 Kudos
Hi Shashank,
Dio
Explorer
0 Kudos

Hi,

My requirement was need do subtotal and grand total in itab, I'll do this in display before and I'm using this code:

wa_fieldcat-fieldname  = 'WRBTR'.    " Fieldname in the data table
  wa_fieldcat-seltext_m  = 'Amount Invoice'.   " Column description in the output
  wa_fieldcat-do_sum     = 'X'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_sort-spos      = 1.
  wa_sort-fieldname = 'ZNME1'.
  wa_sort-up        = 'X'.
  wa_sort-subtot    = 'X'.
  APPEND wa_sort TO it_sort.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      it_fieldcat   = it_fieldcat
      it_sort       = it_sort
    TABLES
      t_outtab      = it_data
    EXCEPTIONS
      program_error = 1

Regards,
Dio