Application Development and Automation 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: 
Read only

how to use aggregate function on internal table

0 Likes
4,135

hi experts,

I am beginner in abap.I want to use sum function on internal table.

I have structure as follow:

types: begin of ty_ftab,

       docno TYPE bkpf-belnr,

       comcode TYPE bkpf-bukrs,

       year TYPE bkpf-gjahr,

       line_itm type bsad-buzei,

       cust type bsad-kunnr,

       amt type bsad-dmbtr,

end of ty_ftab.

data: it_ftab type table of ty_ftab,

           wa_ftab type ty_ftab.

i fetched data successfully into it_ftab from bkpf and bsad table and displyed into alv.

now i want sum of amt using group by cust and want to display like

cust        total_amt      

in next screen...

displying part is not important but how can i do sum of amt according to cust value? Is there in way to query on internal table?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,692

Hi Tushar,

Defind one more internal table and work area of two fields customer and amount.

loop on your existing table assign to final tables work area and use COLLECT statment.

Loop at X[Existing] into wa.

wa_final-kunnr = wa-KUNNR.

wa_final-amount = wa-amt.

collect wa_final to it_final.

Endloop.

Display this it_final into next screen.

Thanks,

Dharmishta

5 REPLIES 5
Read only

Former Member
0 Likes
1,693

Hi Tushar,

Defind one more internal table and work area of two fields customer and amount.

loop on your existing table assign to final tables work area and use COLLECT statment.

Loop at X[Existing] into wa.

wa_final-kunnr = wa-KUNNR.

wa_final-amount = wa-amt.

collect wa_final to it_final.

Endloop.

Display this it_final into next screen.

Thanks,

Dharmishta

Read only

0 Likes
1,692

hi Dharmishta,

Thanx for the valuable reply.

Your idea is working fine.

Thanks,

Tushar

Read only

Former Member
0 Likes
1,692

Hi,

try this code,

data : i_sort  TYPE TABLE OF slis_sortinfo_alv

  w_sort like LINE OF i_sort.

sort it_ftab by cust.

w_sort-subtot = 'X'.

w_sort-fieldname = 'AMT'.

w_sort-tabname = 'it_ftab'.

APPEND w_sort to i_sort.

In fieldcatalog :

w_fcat-fieldname = 'AMT'.

w_fcat-tabname  = 'it_ftab'.

w_fcat-do_sum = 'X'.

In REUSE_ALV_GRID_DISPLAY  FUNCTION MODULE  provide the it_sort.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

*  EXPORTING

*    I_INTERFACE_CHECK                = ' '

*    I_BYPASSING_BUFFER                = ' '

*    I_BUFFER_ACTIVE                  = ' '

*    I_CALLBACK_PROGRAM                = ' '

*    I_CALLBACK_PF_STATUS_SET          = ' '

*    I_CALLBACK_USER_COMMAND          = ' '

*    I_CALLBACK_TOP_OF_PAGE            = ' '

*    I_CALLBACK_HTML_TOP_OF_PAGE      = ' '

*    I_CALLBACK_HTML_END_OF_LIST      = ' '

*    I_STRUCTURE_NAME                  =

*    I_BACKGROUND_ID                  = ' '

*    I_GRID_TITLE                      =

*    I_GRID_SETTINGS                  =

*    IS_LAYOUT                        =

*    IT_FIELDCAT                      =

*    IT_EXCLUDING                      =

*    IT_SPECIAL_GROUPS                =

    IT_SORT                          =  i_sort

*    IT_FILTER                        =

*    IS_SEL_HIDE                      =

*    I_DEFAULT                        = 'X'

*    I_SAVE                            = ' '

*    IS_VARIANT                        =

*    IT_EVENTS                        =

*    IT_EVENT_EXIT                    =

*    IS_PRINT                          =

*    IS_REPREP_ID                      =

*    I_SCREEN_START_COLUMN            = 0

*    I_SCREEN_START_LINE              = 0

*    I_SCREEN_END_COLUMN              = 0

*    I_SCREEN_END_LINE                = 0

*    I_HTML_HEIGHT_TOP                = 0

*    I_HTML_HEIGHT_END                = 0

*    IT_ALV_GRAPHICS                  =

*    IT_HYPERLINK                      =

*    IT_ADD_FIELDCAT                  =

*    IT_EXCEPT_QINFO                  =

*    IR_SALV_FULLSCREEN_ADAPTER        =

*  IMPORTING

*    E_EXIT_CAUSED_BY_CALLER          =

*    ES_EXIT_CAUSED_BY_USER            =

    TABLES

      t_outtab                          =

*  EXCEPTIONS

*    PROGRAM_ERROR                    = 1

*    OTHERS                            = 2

            .

  IF sy-subrc <> 0.

* Implement suitable error handling here

  ENDIF.

Read only

narendar_naidu
Active Participant
0 Likes
1,692

Hi

Either use Control break statements and use SUM or collect statement , depending on your requirement.

control break statemnts.

AT-NEW.AT-END,AT-LAST,AT-FIRST,ONCHANGE etc.

regards,

Read only

Former Member
0 Likes
1,692

Hi Tushar,

Do you want to display the sum in the ALV ? if so then follow the below steps:-

  wa_sort-spos = '01' .
  wa_sort-fieldname = 'CUST'  "sort based on the customer as you want sum based on cust.
  wa_sort-tabname = 'IT_FTAB'.
  wa_sort-up = 'X'.
  wa_sort-subtot = 'X'.
  APPEND wa_sort TO i_sort .


Then pass the i_sort to the importing parameter of the alv.

Alternatively you can use control level processing and can use the event AT END. In this you need to do the manual sum inside that event.

Let me know if you need some more inputs.