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: 

Small and simple code required

Former Member
0 Kudos

Hi

I have following requirement.

Internal table ITAB1 contains following values(ITAB1 is Sorted on F1,F2,F3)

F1 F2 F3 F4

-


1 2 2 10

1 2 2 20

1 2 2 15

1 3 4 18

1 3 4 2

2 3 5 10

And Resultant ITAB2 must contain below values

F1 F2 F3 F4

-


1 2 2 45

1 3 4 20

2 3 5 10

(i.e The column F4 contains SUM of records having similar F1 F2 and F3)

Can any body provide me with the code?

Reward points will be surely given..

Thanks

1 ACCEPTED SOLUTION

Former Member
0 Kudos

TAKE ITAB2 LIKE ITAB1.

LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY F1 = ITAB1-F1 F2 = ITAB1-F2 F3 = ITAB1-F3.

IF SY-SUBRC = 0.

ITAB2-F4 = ITAB1-F4 + ITAB2-F4.

MODIFY ITAB2 INDEX SY-TABIX.

ELSE.

MOVE-CORRESPONDING ITAB1 TO ITAB2.

APPEND ITAB2.

ENDIF.

ENDLOOP.

HERE BOTH INT TABLE WITH HEADER LINE IF YOU HAVE WITHOUT HEADER LINE YOU HAVE TO USE WORK AREA TO DO ALL THE OPERATION.

REGARDS

SHIBA DUTTA

9 REPLIES 9

Former Member
0 Kudos

Hi

Use COLLECT statament: if there isn't a record with the same key it'll append the record else it'all update only the numeric fields calculating the total.

LOOP AT ITAB1.
  ITAB2-F1 = ITAB1-F1.
  ITAB2-F2 = ITAB1-F2.
  ITAB2-F3 = ITAB1-F3.
  
  ITAB2-F4 = ITAB1-F4.
  COLLECT ITAB2.
ENDLOOP.

The fields F1, F2 and F3 of ITAB2 has to be char, in this way the system'll calculate the sum for field F4 only.

Max

Bema
Active Participant
0 Kudos

hi

COLLECT [wa INTO] itab.

When you append the records in to the internal table,

use can use COLLECT stmts

0 Kudos

Hi Max

Thanks for the suggestion

But the Collect works with Numeric fields

But in my ITAB1 field F4 is a Quantity type field..

Please consider this...

Former Member
0 Kudos

Hi,

Try this logic:

loop at itab.

at new f3. "assumed itab sorted on f1 f2 f3.
vf1 = itab-f1.
vf2 = itab-f2.
vf3 = itab-f3.
sum.
vf4 = itab-f4.

insert into itab2 values (vf1 vf2 vf3 vf4).
endat.
endloop.

Jogdand M B

Former Member
0 Kudos

Hi tulip,

do you need the sum for the field f1 f2 f3 in f4 ..then it can be done this way..

data: v_var type i.

loop at itab1.

v_var = iab1-f1 + itab1-f2 + itab1-f3.

itab1-f4 = v_var.

modify itab1 transporting f4.

clear v_var.

endloop.

Repeat the same for itab2.

if you want to delete the duplicate entries then use

SORT itab1 by f1 f2 f3.

Regards,

Jayant

Former Member
0 Kudos

TAKE ITAB2 LIKE ITAB1.

LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY F1 = ITAB1-F1 F2 = ITAB1-F2 F3 = ITAB1-F3.

IF SY-SUBRC = 0.

ITAB2-F4 = ITAB1-F4 + ITAB2-F4.

MODIFY ITAB2 INDEX SY-TABIX.

ELSE.

MOVE-CORRESPONDING ITAB1 TO ITAB2.

APPEND ITAB2.

ENDIF.

ENDLOOP.

HERE BOTH INT TABLE WITH HEADER LINE IF YOU HAVE WITHOUT HEADER LINE YOU HAVE TO USE WORK AREA TO DO ALL THE OPERATION.

REGARDS

SHIBA DUTTA

0 Kudos

Thanks Shiba Let me check with your suggestion...

0 Kudos

Hi,

Collect will work for Quantity fields also. So, you can go ahead with Max's logic.

Regards

Sailaja.

Former Member
0 Kudos

Your problem can solve with using AT NEW.

First u will first fill all values into ITAB1.

Loop at ITAB1.

AT NEW ITAB1-f1,f2,f3.

collect itab-f4.

endat.

I hope it will work.

Please give points if it is works.