Former Member

### Summing value in Internal Table

Hi Abapers,

I have some problem during manipulating the internal table:

The scenario is like this

I have one internal Table:

Material Curr PO Quantity

A EUR 10

A EUR 15

A GBP 20

A GBP 10

B USD 10

Now I have to Sum the POQty and put this in other field Sum of Internal Table on the basis of grouping materail as well as Curr.

For Material A and Curr EUR, i should get sum of PO Qty 25 and

for material A and Curr GBP, i shld get sum of PO Qty 30 and

For B - USD, i shld get 10 in Sum field.

How to do this in ABAP Code?

Regards,

Rahul Sinha

10|10000 characters needed characters exceeded

• Former Member
Dec 22, 2009 at 01:20 PM

Hi Rahul,

This is quite simple. We can use COLLECT and it will sum automatically.

1. Declare another internal table, eg. STAB

with three fields only

Material

Curr

PO Quantity

2. Not loop at your original internal table say ITAB, and write like this.

```
loop at itab.

move-corresponding itab to stab.

COLLECT STAB.

Endloop.

```

I assume STAB and ITAB have same field names, hence i have written move-corresponding.

Based upon PO and CURR combination, (ie. DISTINCT combination of alphanumeric fields in STAB),

collect will do the sum automatically.

regards,

amit m.

10|10000 characters needed characters exceeded
• Former Member
Dec 22, 2009 at 01:20 PM

Hello Rahul ,

Solution 1 :-Using own logic

Just use an on change of at the PO field ....

On change of (PO field) , sum the quantity and put it ...

Solution 2:Using COLLECT statement

I think there is no need of on change ..you could use the COLLECT keyword

K.Sibi

Edited by: sibi k kanagaraj on Dec 22, 2009 2:20 PM

Edited by: sibi k kanagaraj on Dec 22, 2009 2:22 PM

10|10000 characters needed characters exceeded
• Former Member
Dec 22, 2009 at 01:24 PM

Hi Rahul,

While Appending the data to internal table use COLLECT statement instead of APPEND..

COLLECT statement will add the quantity fields if the other fields are same ie, material and currency fields in your case.

So for your case, internal table will have data when you use COLLECT statement

A EUR 25

A GBP 30

B USD 10