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

Internal Table Line Operations - Collect Statement

Former Member
0 Likes
775

Hello Gurus,

I have an internal table with data in it. The fields of the internal table are

Location - Matnr - Qty - Flag.

Same material can be at different locations with different quantities. I need to get material and total quantity of the material in all the locations in a new internal table.

If the table (it_tab) contains -

Location - matnr - qty - flag

1200 abc 10 S

1201 abc 20 S

1205 abc 30 S

1207 abc 50 S

1200 xyz 20 S

1201 xyz 25 S

1300 xyz 22 S

From this table, I need to get the result int table (it_res) as below -

Matnr - qty

abc 110

xyz 67

110 = 102030+50

67 = 252220

My idea is we can use collect statement looping the it_tab table.

Please help me how to do this.

Regards,

Balu

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
752

Hi balu,

1. Simple

2. Create one another internal table STAB,

with only two fields

a) MATNR

b) QTY

3. Now,

4.

Loop at Itab.

Move-corresponding itab to STAB.

COLLECT STAB.

Endloop.

regards,

amit m.

Hello Gurus,

I have an internal table with data in it. The fields of the internal table are

Location - Matnr - Qty - Flag.

Same material can be at different locations with different quantities. I need to get material and total quantity of the material in all the locations in a new internal table.

If the table (it_tab) contains -

Location - matnr - qty - flag

1200 abc 10 S

1201 abc 20 S

1205 abc 30 S

1207 abc 50 S

1200 xyz 20 S

1201 xyz 25 S

1300 xyz 22 S

From this table, I need to get the result int table (it_res) as below -

Matnr - qty

abc 110

xyz 67

110 = 102030+50

67 = 252220

My idea is we can use collect statement looping the it_tab table.

Please help me how to do this.

Regards,

Balu

5 REPLIES 5
Read only

Former Member
0 Likes
752

Hi,

Please try this.


sort it_tab by matnr ascending.

loop at it_tab.
  it_res-matnr = it_tab-matnr.
  it_res-qty = it_tab-qty.
  collect it_res.
endloop.

...

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
753

Hi balu,

1. Simple

2. Create one another internal table STAB,

with only two fields

a) MATNR

b) QTY

3. Now,

4.

Loop at Itab.

Move-corresponding itab to STAB.

COLLECT STAB.

Endloop.

regards,

amit m.

Read only

0 Likes
752

Thanks Ferry and Amit.

For some reason I am not getting the required out put.

I am getting the output as single entries for the repeated materials but the quantity is not being added up.

loc - mat - qty - flag

10 abc 10 s

20 xyz 10 s

30 abc 10 s

40 xyz 10 s

50 abc 50 s

30 xyz 50 s

I am getting the output as -

mat - qty

abc 10

abc 50

xyz 10

xyz 50

But I need as

abc 70

xyz 70

Any suggestions.

Regards,

Balu

Read only

0 Likes
752

Hi Balu,

If you want the qty to be added, I believe that the field in your internal table should be defined as a numeric field.

Hope it helps.

Regards,

Gilberto Li

Read only

Former Member
0 Likes
752

Thanks.