‎2009 Feb 23 9:53 AM
Hi guys,
I have an intenal table for suppose say 5 records with duplicate entrys.
My ITAB looks some thing like
TKNUM weight volume
1 10 5
1 8 6
1 15 10
2 30 15
2 20 9
I need to calculate total weight and volume for each TKNUM (Shipment number) i.e,
After calculation my new internal table should look like
TKNUM Total Weight Total Volume
1 33 21
2 50 24
How to do this.
Thanks in Advance.
Prasad.
‎2009 Feb 23 10:10 AM
sort itab by tknum.
loop at itab
into wa_itab.
at end of tknum.
sum.
append wa_itab to itab1.
endat.
endloop.
Regards
Varun
‎2009 Feb 23 10:01 AM
Hi:
under the loop
total for volume = + volume.
total for weight = + weight.
Regards
Shashi
‎2009 Feb 23 10:06 AM
Hi,
Here is the solution.
data : begin of ty occurs 0,
f1 type i,
f2 type i,
f3 type i,
end of ty.
ty-f1 = 1.
ty-f2 = 10.
ty-f3 = 5.
append ty.
ty-f1 = 1.
ty-f2 = 8.
ty-f3 = 6.
append ty.
ty-f1 = 1.
ty-f2 = 15.
ty-f3 = 10.
append ty.
ty-f1 = 2.
ty-f2 = 30.
ty-f3 = 15.
append ty.
ty-f1 = 2.
ty-f2 = 20.
ty-f3 = 9.
append ty.
loop at ty.
at end of f1.
sum.
write : / ty-f1, ty-f2, ty-f3.
endat.
endloop.
‎2009 Feb 23 10:09 AM
Hi jayanti,
I mean to say that i dont have same data in internal table it keeps on changing.
I need to calculate the total weight and volume for that shipment.
Thanks,
Prasad.
‎2009 Feb 23 10:12 AM
Hi,
As long as you have a key field, this solution will work.I just had taken an example.
For the key field, use
loop ...
at end of TKNUM.
sum.
write :....
endat.
endloop.
‎2009 Feb 23 10:09 AM
Hi friend,
Use COLLECT statements.
Collect itab.
See F1 help on COLLECT
Thanks..
Edited by: Sap Fan on Feb 23, 2009 11:10 AM
‎2009 Feb 23 10:09 AM
Hi Prasad,
Use the following logic.
I assume your internal table name is IT_OLD which has the follwing data.
TKNUM weight volume
1 10 5
1 8 6
1 15 10
2 30 15
2 20 9
No do the following:
data: it_new like it_old occurs 0 with header line.
SORT IT_OLD by TKNUM.
LOOP AT IT_OLD.
AT NEW TKNUM.
SUM.
MOVE-CORRESPONDING IT_OLD TO IT_NEW.
APPEND IT_NEW.
ENDAT.
ENDLOOP.
Check the contents of IT_NEW after the loop for the results.
Regards,
Prakash Pandey
‎2009 Feb 23 10:16 AM
Hi Prakash,
Thanks for your reply.
One more small information is I have 1 other fileds with other data.
Means
TKNUM weight volume x
1 10 5 abc
1 8 6 def
1 15 10 ac
2 30 15 bc
2 20 9 zz
I dont need this x data in the newly calculated internal table.
Then how to eliminate this...
Thanks,
Prasad.
‎2009 Feb 23 10:20 AM
Dont move it ... thats it...
Try using powerful statements ex: 'collect' for this scenario.
‎2009 Feb 23 10:10 AM
sort itab by tknum.
loop at itab
into wa_itab.
at end of tknum.
sum.
append wa_itab to itab1.
endat.
endloop.
Regards
Varun
‎2009 Feb 23 10:14 AM
‎2009 Feb 23 10:15 AM
if tknum is of datatype char.
then
declare itab2 same as itab1.
loop at itab1 into wa.
collect wa into itab2.
endloop.
if not char type go for control break statements
‎2009 Feb 23 10:17 AM
Hi Prasad,
use collect statement to append the records..
declare an internal table with the duplicate entries col as a character field..
types: begin of type_s_tab,
col1 type c,
col2 type i,
col3 type i,
end of type_s_tab.
data fs_tab type type_s_tab.
data: itab like table of fs_tab with header line.
itab-col1 = 1.
itab-col2 = 10.
itab-col3 = 5.
append itab.
itab-col1 = 1.
itab-col2 = 8.
itab-col3 = 6.
collect itab.
loop at itab.
write:/ itab-col1,
itab-col2,
itab-col3.
endloop.
Regards,
Mdi.Deeba
‎2009 Feb 23 10:20 AM
Hi,
loop at itab.
at end of tknum.
sum.
write : / itab-tknum, itab-weight, itab-volume.
endat.
endloop.
‎2009 Feb 23 10:23 AM
Use COLLECT stmt.
Data :
it_tab type standard table of t_your_structure, //your internal table having 5 records.
it_tab1 type standard table of t_your_structure, // internal table after calculation.
wa_tab type t_your_structure.
Loop at it_tab into wa_tab.
collect wa_tab into it_tab1.
endloop.
‎2009 Feb 23 10:37 AM
Hi guys,
I have an intenal table for suppose say 5 records with duplicate entrys.
My ITAB looks some thing like
TKNUM weight volume
1 10 5
1 8 6
1 15 10
2 30 15
2 20 9
I need to calculate total weight and volume for each TKNUM (Shipment number) i.e,
After calculation my new internal table should look like
TKNUM Total Weight Total Volume
1 33 21
2 50 24
How to do this.
Thanks in Advance.
Prasad.
Hi Prasad,
You can use following code.
SORT itab BY tknum weight volume.
DATA: lv_itab LIKE itab ,
lv_weight LIKE itab-weight,
lv_volume LIKE itab-volume.
LOOP AT itab.
move itab to lv_itab.
lv_weight = lv_weight + itab-weight.
lv_volume = lv_volume + itab-volume.
AT END OF
itab1-tknum = lv_itab-tknum.
itab1-weight = lv_weight.
itab1-volume = lv_volume.
append itab1.
clear: itab1 ,
lv_weight,
lv_volume.
ENDAT.
ENDLOOP.
Hope this help you.
Regards,
Anil
‎2009 Feb 23 10:45 AM