2007 May 15 10:01 AM
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
2007 May 15 10:11 AM
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
2007 May 15 10:03 AM
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
2007 May 15 10:08 AM
hi
COLLECT [wa INTO] itab.
When you append the records in to the internal table,
use can use COLLECT stmts
2007 May 15 10:10 AM
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...
2007 May 15 10:08 AM
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
2007 May 15 10:09 AM
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
2007 May 15 10:11 AM
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
2007 May 15 10:24 AM
2007 May 15 10:26 AM
Hi,
Collect will work for Quantity fields also. So, you can go ahead with Max's logic.
Regards
Sailaja.
2007 May 15 11:18 AM
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.