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: 

sum in itab

Former Member
0 Kudos
637

i have 2 itabs

1. lt_lbkum (matnr,lbkum)

2.m_itab (matnr,stock)

i need to make sum per matnr and takes the biggest sum

between those 2 (i need good performance too)

1 ACCEPTED SOLUTION

LucianoBentiveg
Active Contributor
0 Kudos
197

Use COLLECT instead APPEND to add rows to Internal tables.

Regards.

7 REPLIES 7

LucianoBentiveg
Active Contributor
0 Kudos
198

Use COLLECT instead APPEND to add rows to Internal tables.

Regards.

Former Member
0 Kudos
197

Hi,

You can use COLLECT statement.

Regards,

Shashank

Former Member
0 Kudos
197

Hello,

sort IT_IBKUM by matnr.

LOOP AT IT_IBKUM.

at end of matnr.

sum.

endat.

lv_ibkum = it_ibkum.

endloop.

sort IT_itab by matnr.

LOOP AT IT_itab.

at end of matnr.

sum.

endat.

lv_stock = it_itab.

endloop.

if lv_ibkum > lv_stock.

temp. = lv_ibkum.

else.

temp = lv_tock.

endif.

If useful.

Reward the points.

Regards,

vasanth

Former Member
0 Kudos
197

Hai Rani

Use collect statement for sum of two internal tables

I am giving you the syntax for Collect statement

COLLECT

Basic form

COLLECT [wa INTO] itab.

Addition

... SORTED BY f

Effect

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .

After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.

Notes

COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.

If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that

the internal table will actually be unique or compressed, as described above and

COLLECT will run very efficiently.

If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.

Example

Compressed sales figures for each company

DATA: BEGIN OF COMPANIES OCCURS 10,

NAME(20),

SALES TYPE I,

END OF COMPANIES.

COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 10.

COLLECT COMPANIES.

COMPANIES-NAME = 'Tiger'. COMPANIES-SALES = 20.

COLLECT COMPANIES.

COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 30.

COLLECT COMPANIES.

The table COMPANIES now has the following appearance:

NAME SALES

Duck 40

Tiger 20

Addition

... SORTED BY f

Effect

COLLECT ... SORTED BY f is obsolete and should no longer be used. Use APPEND ... SORTED BY f which has the same meaning.

Note

Performance

The cost of a COLLECT in terms of performance increases with the width of the default key needed in the search for table entries and the number of numeric fields with values which have to be added up, if an entry is found in the internal table to match the default key fields.

If no such entry is found, the cost is reduced to that required to append a new entry to the end of the table.

A COLLECT statement used on a table which is 100 bytes wide and has a key which is 60 bytes wide and seven numeric fields is about approx. 50 msn (standardized microseconds).

Note

Runtime errors

COLLECT_OVERFLOW : Overflow in integer field when calculating totals.

COLLECT_OVERFLOW_TYPE_P : Overflow in type P field when calculating totals.

Thanks & regards

Sreeni

Former Member
0 Kudos
197

U MAY USE COLLECT STATEMENT FOR THIS.

AND COMPARE THE RESULT AND MODIFY ANY ONE ON THE BASE OF COMPARISION .

Former Member
0 Kudos
197
sort lt_lbkum by matnr lbkum.
loop at lt_lbkum.
  at end of matnr.
   sum.
  endat.
endloop.

sort m_itab by matnr stock.
loop at m_itab.
  at end of matnr.
   sum.
  endat.
endloop.

sort lt_lbkum by lbkum descending.
sort m_itab by stock descending.

read table lt_lbkum index 1.
read table m_itab index 1.

if lt_lbkum-lbkum LT m_itab-stock.
   m_itab-stock is MAX.
else.
  lt_lbkum-lbkum is greater.
endif.

Former Member
0 Kudos
197

Hi rani,

1.

*----


ITAB

1 - 10

1 - 20

PTAB

1 - 10

1 - 21

*----


FINAL OUTPUT

1 - 31

2. Just copy paste

(the name of internal tables are different)

(for the logic, i had to create

3 EXTRA internal tables)

3.

report abc.

*----


DATA : BEGIN OF ITAB OCCURS 0,

MATNR LIKE MARA-MATNR,

LBKUM TYPE I,

END OF ITAB.

DATA : BEGIN OF PTAB OCCURS 0,

MATNR LIKE MARA-MATNR,

STOCK TYPE I,

END OF PTAB.

*----


EXTRA INTERNAL TABLE

data : sumitab like table of itab with header line.

data : sumptab like table of Ptab with header line.

data : FINALtab like table of itab with header line.

*----


ITAB-MATNR = 1.

ITAB-LBKUM = 10.

APPEND ITAB.

ITAB-MATNR = 1.

ITAB-LBKUM = 20.

APPEND ITAB.

PTAB-MATNR = 1.

PTAB-STOCK = 10.

APPEND PTAB.

PTAB-MATNR = 1.

PTAB-STOCK = 21.

APPEND PTAB.

*----


  • LOGIC

*----


*--- (1)

loop at itab.

sumitab = itab.

collect sumitab.

endloop.

*--- (2)

loop at ptab.

sumptab = ptab.

collect sumptab.

endloop.

*--- (3)

loop at sumitab.

clear finaltab.

finaltab = sumitab.

read table sumptab with key matnr = sumitab-matnr.

if sy-subrc = 0.

if sumptab-stock > finaltab-lbkum.

finaltab-lbkum = sumptab-stock.

endif.

endif.

append finaltab.

endloop.

break-point.

regards,

amit m.