Former Member

# ALV - need to sum values of internal table and display in ALV

I have data in internal table as:

Material date sum1 sum2

Mat_A 19990101 4 4

Mat_A 20080501 3 0

Mat_A 20080601 2 0

Mat_B 19990101 2 0

Mat_B 20080601 5 5

Required output is :

Material qty1 qty2 19990101 20080501 20080601

Mat_A 432 4 4 3 2

Mat_B 2+5 5 2 5

Thinking of using ALV to pass the internal table and display as classical report (and also to save as excel spreadsheet).

Counting your help on the following questions:

1) How to accomplish the sum in ALV report? Can ALV FM do that or one has to use ABAP to compute the sum from the given internal table?

2) Mat_A can have more date values. Here it got 3 distinct date values 19990101, 20080601, 20080501. If it has say 5 date values, how to create the ALV date columns (from 3 to 5 date columns) dynamically?

Thanks for the help.

10|10000 characters needed characters exceeded

### Related questions

Former Member
Posted on Jun 14, 2008 at 02:57 PM

for the sum inalv we use generally..

it_fieldcat-do_sum = 1.

check this examples...

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

&----

*& Report ZTEST_ALV_PERC_13317

*&

&----

*&

*&

&----

REPORT ztest_alv_perc_13317.

TYPE-POOLS: slis.

DATA: it_fieldcat TYPE slis_t_fieldcat_alv,

wa_fieldcat TYPE slis_fieldcat_alv,

it_events TYPE slis_t_event,

wa_events TYPE slis_alv_event,

it_sort TYPE slis_t_sortinfo_alv,

wa_sort TYPE slis_sortinfo_alv,

l_layout TYPE slis_layout_alv.

TYPES: BEGIN OF ty_itab,

field1(10),

qty1 TYPE i,

qty2 TYPE i,

qty3 TYPE i,

dummy TYPE c,

END OF ty_itab.

DATA: itab TYPE STANDARD TABLE OF ty_itab WITH HEADER LINE,

itab1 TYPE ty_itab.

START-OF-SELECTION.

itab-field1 = 'FIRST'.

itab-qty1 = 2.

itab-qty2 = 1.

itab-qty3 = 5.

itab-dummy = 10.

APPEND itab.

itab-field1 = 'FIRST'.

itab-qty1 = 2.

itab-qty2 = 1.

itab-qty3 = 5.

itab-dummy = 10.

APPEND itab.

itab-field1 = 'FIRST'.

itab-qty1 = 2.

itab-qty2 = 1.

itab-qty3 = 5.

itab-dummy = 10.

APPEND itab.

wa_fieldcat-col_pos = 1.

wa_fieldcat-fieldname = 'FIELD1'.

wa_fieldcat-tabname = 'ITAB'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-col_pos = 2.

wa_fieldcat-fieldname = 'QTY1'.

wa_fieldcat-tabname = 'ITAB'.

wa_fieldcat-do_sum = 'X'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-col_pos = 3.

wa_fieldcat-fieldname = 'QTY2'.

wa_fieldcat-tabname = 'ITAB'.

wa_fieldcat-do_sum = 'X'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-col_pos = 4.

wa_fieldcat-fieldname = 'QTY3'.

wa_fieldcat-tabname = 'ITAB'.

wa_fieldcat-do_sum = 'X'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-col_pos = 5.

wa_fieldcat-fieldname = 'DUMMY'.

wa_fieldcat-tabname = 'ITAB'.

wa_fieldcat-do_sum = 'X'.

wa_fieldcat-no_out = 'X'.

APPEND wa_fieldcat TO it_fieldcat.

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

EXPORTING

i_list_type = 0

IMPORTING

et_events = it_events

EXCEPTIONS

list_type_wrong = 1

OTHERS = 2.

IF sy-subrc <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = sy-repid

it_fieldcat = it_fieldcat

TABLES

t_outtab = itab

EXCEPTIONS

program_error = 1

OTHERS = 2

.

IF sy-subrc <> 0.

ENDIF.

10|10000 characters needed characters exceeded
• Former Member
Posted on Jun 14, 2008 at 02:45 PM

Data in internal table

Material, date, sum1, sum2

Mat_A, 19990101, 4, 4

Mat_A, 20080501, 3, 0

Mat_A, 20080601, 2, 0

Mat_B, 19990101, 2, 0

Mat_B, 20080601, 5, 5

Needed output:

Material, qty1, qty2, 19990101, 20080501, 20080601

Mat_A, 432, 4, 4, 3, 2

Mat_B, 2+5, 5, 2, 0, 5

10|10000 characters needed characters exceeded
• Former Member
Posted on Jun 14, 2008 at 02:54 PM

Hi,

```*--declear this
Data: I_SORT TYPE SLIS_T_SORTINFO_ALV,
WA_SORT TYPE SLIS_SORTINFO_ALV.

*---Sort
WA_SORT-SPOS = '1'.
WA_SORT-FIELDNAME = 'MATNR'.(Material number)
WA_SORT-UP = 'X'.
WA_SORT-SUBTOT = 'X'.
APPEND WA_SORT TO I_SORT.
CLEAR WA_SORT.

Pass this in ALV Function Module,
IT_SORT = I_SORT```

This will work

Thanks

Vikranth

10|10000 characters needed characters exceeded
• Former Member
Posted on Jun 14, 2008 at 02:58 PM

hi,

while creating the field catalog, provide the value 'X' to the field DO_SUM of field catalog to which fields of internal table.

type-pools: slis.

data: wa_fcat type slis_fieldcat_alv,

it_fcat type slis_t_fieldcat_alv.

wa_fcat-fieldname = '<field-name>'.

wa_fcat-ref_tabname = '<refarence table name>'.

wa_fcat-ref_fieldname = '<refarence fieldname>.

wa_fcat-do_sum = 'X'.

APPEND wa_fieldcat TO it_fieldcat.

repeat the same procedure for all the internal table field for which you have to provide SUM.

thanks&regards,

Ashok

10|10000 characters needed characters exceeded
• Former Member Former Member

hi,

hi,

while creating the field catalog, provide the value 'X' to the field DO_SUM of field catalog to which fields of internal table.

type-pools: slis.

data: wa_fcat type slis_fieldcat_alv,

it_fcat type slis_t_fieldcat_alv.

wa_fcat-fieldname = '<field-name>'.

wa_fcat-ref_tabname = '<refarence table name>'.

wa_fcat-ref_fieldname = '<refarence fieldname>.

wa_fcat-do_sum = 'X'.

APPEND wa_fieldcat TO it_fieldcat.

repeat the same procedure for all the internal table field for which you have to provide SUM.

thanks&regards,

Ashok

Hope Ashok solution will help you to do sub-total,in order to get column name dynamically depending upon the number of dates in the internal table ,try the following logic,

check the following link,before getting into this simple logic ..

How to add a field in a field catalog

MY LOGIC.

fieldcatalog-fieldname = 'DATE'.

LOOP AT ITAB1.

FIELDCATALOG-SELTEXT_M = ITAB1-DATE.

FIELCATALOG-COL_POS = 0 * Increment this value everytime using COUNTER

fieldcatalog-outputlen = 10.

fieldcatalog-emphasize = 'X'.

fieldcatalog-key = 'X'.

• fieldcatalog-do_sum = 'X'.

• fieldcatalog-no_zero = 'X'.

append fieldcatalog to fieldcatalog.

clear fieldcatalog.

ENDLOOP.

So each time,whenever you are looping your internal table,date value is fetched and appended to your fieldcatalog internal table...and you can use this fieldcatalog table to display the column heading dynamically in the FM.

FM specifically for Fieldcatalog

Thanks

Siva