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: 

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

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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.

6 REPLIES 6

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

0 Kudos

Thanks to all. Assigned points as well. I will try and update the results.

I have little complicated requirement where the number of date fields in input is not consistent. It can vary based on the number of months data in SAP R/3. I tried to summarize the requirement below, please let me know how to achieve the result in ALV. Thanks again.

(1) Internal table : input to ALV

Material, date, sum1, sum2

Mat_A, 19990101, 0, 4

Mat_A, 20080101, 1, 0

Mat_A, 20080201, 2, 0

Mat_A, 20080301, 3, 0

Mat_A, 20080401, 4, 0

Mat_A, 20080501, 5, 0

Mat_A, 20080601, 6, 0

....

Mat_A, 20081001, 10, 0

(2) Desired output in ALV:

Material, qty1,qty2, 19990101, 20080101, 20080201, 20080301,......,20081001

Mat_A, (0123...9),(400..+0),4,1,2,3,...10

0 Kudos

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 ..

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