‎2009 Sep 18 5:56 AM
Hello All,
I have a internal table with material number,material size and cartonwgt fields.
For the combination of mat.no and size there are different carton weights.So I want to add all the carton weights where mat no and size is same.I thought of using collect statement.Pls can ny one explain how to use that or is there any other solution for this?
Thanks,
Kumar.
‎2009 Sep 18 6:07 AM
Hello Kumarn290,
Collect statement is used to store records in internal table while summing up their numeric fields. So it will serve your requirement. Simply read each record in an work area and then use "COLLECT WA INTO ITAB" so that you will get expected result.
Thanks & Regards,
Vaibhav Pendse
‎2009 Sep 18 6:00 AM
Hi,
Use this example.
if you have data in internal table itab.
then declare itab1 same as itab.
and then.
Loop at itab.
collect itab into itab1.
Endloop.
Regards,
Vijay
‎2009 Sep 18 6:01 AM
Hi,
Yes you can use collect statement. Just loop at the table and use collect. Make sure that the fields which you sum up are numeric and others are type c.
loop at itab.
collect itab.
endloop.
‎2009 Sep 18 6:04 AM
Hi
DATA : BEGIN OF ITAB OCCURS 0,
COL1(3) TYPE C,
COL2 TYPE I,
END OF ITAB.
ITAB-COL1 = 'ABC'.
ITAB-COL2 = 50.
COLLECT ITAB.
ITAB-COL1 = 'ABC'.
ITAB-COL2 = 100.
COLLECT ITAB.
ITAB-COL1 = 'ABC'.
ITAB-COL2 = 150.
COLLECT ITAB.
ITAB-COL1 = 'PQR'.
ITAB-COL2 = 100.
COLLECT ITAB.
ITAB-COL1 = 'PQR'.
ITAB-COL2 = 100.
COLLECT ITAB.
ITAB-COL1 = 'XYZ'.
ITAB-COL2 = 200.
COLLECT ITAB.
LOOP AT ITAB.
WRITE : / ITAB-COL1, ITAB-COL2.
ENDLOOP.
SORT ITAB.
LOOP AT ITAB.
AT FIRST.
WRITE: / 'AT NEW STATEMENT'.
ULINE.
ENDAT.
AT NEW COL1.
WRITE: / ' AT FIRST OF :', ITAB-COL1.
ENDAT.
WRITE : / ITAB-COL1, ITAB-COL2.
AT LAST.
ULINE.
ENDAT.
ENDLOOP.
Regards,
Sreeram
‎2009 Sep 18 6:05 AM
hi kumar,
welcome to SDN.
when a table itab2 is of type
|matnr__ |size__ |weight
chartype|chartype|numeric type
loop at itab into is.
is2- matnr = is-matnr.
is2-size = is-size.
is2-weight = is-weight..
collect is2 into itab2.
endloop.collect keeps the char fields intact and adds the numeric type. please take a note of this
Edited by: Soumyaprakash Mishra on Sep 18, 2009 10:35 AM
‎2009 Sep 18 6:07 AM
Hello Kumarn290,
Collect statement is used to store records in internal table while summing up their numeric fields. So it will serve your requirement. Simply read each record in an work area and then use "COLLECT WA INTO ITAB" so that you will get expected result.
Thanks & Regards,
Vaibhav Pendse
‎2009 Sep 18 6:27 AM
Hello All,
Thanks a tonn for all your replies My carton weight field is of type QUAN.
Actually I need to frame a formula i.e..,
Per unit net weight = Sum of CARWGT(Carton weight) Divided by sum of MENGE(Quantity)
Where Material & size Value is same .
MENGE(Quantity) is also of same QUAN type..
can ny one give an idea to how to do this..plz
Thanks,
Kumar.
‎2009 Sep 18 6:42 AM
say itab1 has matnr, size, carwgt,menge. which has multiple records for one matnr and size.
and itab2 is what we need of type matnr, size, perunit
now.
sort itab1 by matnr, size.
data: gv_matnr type matnr,
size like itab1-size.
loop at itab1 into is1.
if is1-matnr = gv_matnr and is1-size = gv_size.
gv_wt =gv_wt + is1-carwgt.
gv_men = gv_men + is1-menge.
clear is1.
continue.
endif.
"when a new record comes..
gv_net = gv_wt / gv_men.
is2-matnr = gv_matnr.
is2-size = gv_size.
is2-perunit = gv_net.
append is2 to itab2. " new record inserted.
clear: gv_net, gv_matnr,gv_size, gv_wt, gv_men, is2.
"now for current new records in itab1.
gv_matnr = is1-matnr.
gv_size = is1-size.
clear : is1.
endloop.
‎2009 Sep 18 8:01 AM
Thanks a lot,
But is there a way to use collect statement and solve the issue.So that the performance also wil be improved I guess.
Thanks,
Kumar
‎2009 Sep 18 8:17 AM
the code provided me will do without any performance issue. because any ways you will loop one more time to get the perunit.
‎2009 Sep 18 8:24 AM
HI,
Yes you can use collect statement for better performance here. look at this sample code,
data: begin of itab occurs 0,
matnr(10) type c,
val1 type i,
val2 type i,
end of itab.
data: begin of itab1 occurs 0,
matnr(10) type c,
val1 type i,
val2 type i,
avg(3) type p decimals 2,
end of itab1.
itab-matnr = 'm1'.
itab-val1 = '30'.
itab-val2 = '40'.
append itab.
clear itab.
itab-matnr = 'm1'.
itab-val1 = '30'.
itab-val2 = '40'.
append itab.
clear itab.
itab-matnr = 'm1'.
itab-val1 = '70'.
itab-val2 = '30'.
append itab.
clear itab.
itab-matnr = 'm1'.
itab-val1 = '80'.
itab-val2 = '30'.
append itab.
clear itab.
itab-matnr = 'm2'.
itab-val1 = '70'.
itab-val2 = '10'.
append itab.
clear itab.
itab-matnr = 'm2'.
itab-val1 = '90'.
itab-val2 = '20'.
append itab.
clear itab.
loop at itab.
move-corresponding itab to itab1.
collect itab1.
endloop.
loop at itab1.
itab1-avg = itab1-val1 / itab1-val2.
modify itab1.
endloop.
loop at itab1.
write:/ itab1-matnr, itab1-val1, itab1-val2, itab1-avg.
endloop.
Regards,
Vikranth
‎2009 Sep 18 3:07 PM
>
> HI,
> Yes you can use collect statement for better performance here. look at this sample code,
>
>"FIRST LOOP > loop at itab. > move-corresponding itab to itab1. > collect itab1. > endloop. >"SECOND LOOP > loop at itab1. > itab1-avg = itab1-val1 / itab1-val2. > modify itab1. """ > endloop. >>
do you think this will increase the performance? rather than doing the entire thing in one single loop?
‎2009 Sep 18 5:14 PM
It is atleast better than manually summing up with messy coding rather than let the collect statement do the work for us. I should have said more programmer friendly rather than performance though i dont see any critical performance degradation looping on distinct entries.
‎2009 Sep 23 6:05 AM
Example
TYPES: BEGIN OF COMPANY,
NAME(20) TYPE C,
SALES TYPE I,
END OF COMPANY.
DATA: COMP TYPE COMPANY,
COMPTAB TYPE HASHED TABLE OF COMPANY
WITH UNIQUE KEY NAME.
COMP-NAME = 'Duck'. COMP-SALES = 10. COLLECT COMP INTO COMPTAB.
COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB.
COMP-NAME = 'Duck'. COMP-SALES = 30. COLLECT COMP INTO COMPTAB.
Table COMPTAB now has the following contents:
NAME | SALES
---------------
Duck | 40
Tiger | 20
‎2009 Sep 23 6:29 AM
hi
its better u use AT NEW and AT END OF..
EX :
LOOP.
AT NEW 2ND FIELD.
MOVE IST FIELD TO WA.
2ND FIELD TO WA..
ENDAT.
TAKE A COUNTER VARIBALE .TO GET ADDITION OF ALL WEIGHTS.
AT END OF 2ND FIELD.
APPEND.
ENDAT......
ENDLOOP.
NOW IF THE COMBINATION OF THE FIELDS CHANGE THEN ANOTHER ROW WLD BE APPENDED IN TABLE .
THANKS
DHRUV