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 the quantity when 2 records are exactly same in the internal table.

Former Member
0 Kudos

Hi guyz,

In an Internal table for eg if i have data like this

tv20 | germany | 1.2 | 2.3 | 3.4 | 3

tv20 | germany | 1.2 | 2.3 | - 3.4 | 4

tv20 | germany | 1.2 | 2.3 | 3.4 | 3

then the output should be

tv20 | germany | 1.2 | 2.3 | 3.4 | 6

tv20 | germany | 1.2 | 2.3 | -3.4 | 3

plz advise..

regards

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

Sort the table first & then use COLLECT statement for this requirement.

Search SDN for details.

BR,

Suhas

3 REPLIES 3

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

Sort the table first & then use COLLECT statement for this requirement.

Search SDN for details.

BR,

Suhas

JozsefSzikszai
Active Contributor
0 Kudos

the COLLECT only works if the fields you want to compare are defined as character type fields. In your case it means all fields of the internal table has to be character type, except the last one (you want to sum that field). If this is not the case, you have two options to go:

1. Define all fields as character type (except the last one) and COLLECT

2. Develop a manual COLLECT, roughly:

LOOP AT itab INTO wa.
READ TABLE itab2 INTO wa2 WITH KEY ... = wa-... "here you mention all fields excpet the last one
IF sy-subrc EQ 0.
wa2-... = wa2-... + wa-... "entry found => add the last field
ELSE.
APPEND wa TO itab2. "entry not found => add new line
ENDIF.

Former Member
0 Kudos

Hi,

you can use COLLECT statement.

Before that declare all the fields you want to compare as CHAR and for the last field which you want to add declare it as NUMC.

Mubeen