2014 Mar 19 5:14 PM
Dear Experts,
I have a Qty field which needs to be summed up when material no, po no and size are same in my internal table
Eg: PO no Mat no Size Qty
111 001 L 10
111 001 L 20
needs to be shown as
111 001 L 30
I have tried using the collect statement in several ways but unfortunately ddnt work out. Any suggestions?
2014 Mar 19 8:16 PM
The collect statement sums all numeric non key fields of an internal table so you need to declare material no, po no and size as key fields of you table. Something like:
data: lt_table type standard table of type_structure
with non-unique key matnr, po_no, size,
ls_workarea type type_structure.
collect ls_workarea into lt_table.
should work for you. Where type_structure is a local structure with the type you want a line of you internal table to have.
2014 Mar 19 8:32 PM
Hi Anushka,
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.collect statement when used helps to reduce reduntant data [avoid duplicate record] as when collect used for two records having same [caharcter] fields and and some amount fields differently then it will add the numeric values of both fields and assign the obtained value to the first record fields.if when two records having character fields differently then it will two records separately as two records in internal table.
Please beware collect is an expensive statement.so please do a sort and delete adjacent duplicates before doing so.If you use COLLECT with a work area, the work area must be compatible with the line type of the internal table.
Still struck please go through the online documentation of Collect.
Please post your code incase your still got some doubts.or else please close the thread.
Regards,
Kannan
2014 Mar 19 9:13 PM
Also trying Sorting the internal table first by material no, po no and size. Hope it helps.
2014 Mar 19 10:14 PM
Sort is irrelevant to how a collect statement works. As SAP help explains collect sums all numeric non key fields, so the only things that effect how it works are the key and which fields are numeric (of course the table type can effect efficiency but not the end result).
2014 Mar 20 3:13 AM
Assume itab1 holds the basic data and you have one work are of same type (wa1). Now create another internal table of same type ( itab2) and work area also ( wa2).
LOOP AT itab1 INTO wa1.
LOOP AT itab2 INTO wa2 WHERE material_no = wa1-material_no.
wa2-quantity = wa2-quantity + wa1-quantity.
MODIFY itab2 FROM wa2.
ENDLOOP.
IF sy-subrc NE 0.
APPEND wa1 TO itab2.
ENDIF.
ENDLOOP.
Now check itab2 for your required o/p.
2014 Mar 20 3:37 AM
Although Farid's sugegestion of looping and over two tables and summing would work I don't there is any reason to desist with your original approach of using the collect statement. The collect statment is designed exactly for this purpose and would be more efficient than two nested loops. As described before the folloiwng code should work the correct data elements inserted instead of the descriptions
types: BEGIN OF tys_structure,
po_num type po_num,
mat_no type mat_no,
size type sizw,
qty type qty,
END OF tys_structure.
data: lt_table type standard table of tys_structure
with non-unique key matnr, po_no, size,
ls_workarea type type_structure.
...
...
...
collect ls_workarea into lt_table.
One additional note, It would probably be better to use a unique key of a sorted or hashed table if you only want the results to be aggreagted by material no, po no and size. I suggested a non-unique key of a standard table here because it is more flexible and i don't know what elese you might want to do with this table in your code.
2014 Mar 20 4:45 AM
Hi .
Try this code:
FORM test_01 .
TYPES: BEGIN OF tp_data_1 .
TYPES: ebeln TYPE ekpo-ebeln ,
matnr TYPE ekpo-matnr ,
size TYPE ausp-atwrt ,"I did not find any suitable "size" field
menge TYPE ekpo-menge .
TYPES: END OF tp_data_1 .
TYPES: tp_data_1_tab TYPE TABLE OF tp_data_1 .
DATA: it_detail_1 TYPE tp_data_1_tab .
DATA: st_detail_1 LIKE LINE OF it_detail_1 .
DO 10 TIMES .
st_detail_1-ebeln = '0000000111' .
st_detail_1-matnr = 'matnr_001' .
st_detail_1-size = 'L' .
st_detail_1-menge = '1.5' .
APPEND st_detail_1 TO it_detail_1 .
ENDDO .
DATA: it_totals_1 TYPE tp_data_1_tab .
DATA: st_totals_1 LIKE LINE OF it_totals_1 .
FIELD-SYMBOLS: <st_detail_1> LIKE LINE OF it_detail_1 .
LOOP AT it_detail_1 ASSIGNING <st_detail_1> .
* Both structures are the same this time....
MOVE-CORRESPONDING <st_detail_1> TO st_totals_1 .
COLLECT <st_detail_1> INTO it_totals_1 .
ENDLOOP.
BREAK-POINT .
* Check the values of it_detail_1,it_totals_1
ENDFORM . "test_01
Regards.
2014 Mar 20 4:54 AM
Hi Anushka,
just a simple approach apart from above discussion, add your qty field if po no, mat no and size are same, put it in another variable, use that variable to show in output, then after that use delete adjacent duplicates comparing po no, mat no and size, you will get output as you wants.
2014 Mar 20 5:12 AM
2014 Mar 20 11:43 AM
it_itab_temp[] = it_itab[].(move your internal table data to temp table)
DELETE ADJACENT DUPLICATES FROM it_itab_temp COMPARING matno,pono,size .
DATA:Qty TYPE Qty.
LOOP AT it_itab_temp.
CLEAR:Qty.
LOOP AT it_itab WHERE matno = it_itab_temp-matno and pono = it_itab_temp-pono and
size = it_itab_temp-size. (matno,pono,size wise adding the qty)
ADD it_itab-QtyTO Qty.
ENDLOOP.
it_itab_temp-Qty = Qty.
MODIFY it_itab_temp TRANSPORTING Qty.
ENDLOOP.
CLEAR:it_itab.
it_itab[] = it_itab_temp[].
2014 Mar 20 12:05 PM
Use like this.
itab_temp[] = itab[].
sort itab_temp by mat_no po_no size.
delete adjacent duplicates from itab_temp comparing mat_no po_no size.
LOOP AT itab_temp into ls_tab.
itab_temp_2[] = itab[].
DELETE itab_temp_2 WHERE mat_no NE ls_tab-mat_no.
DELETE itab_temp_2 WHERE po_no NE ls_tab-po_no.
DELETE itab_temp_2 WHERE size NE ls_tab-size.
LOOP AT itab_temp_2.
lv_sum = lv_sum + itab_temp_2-quantity.
ENDLOOP.
MOVE ls_tab TO ls_final.
ls_final-quantity = lv_sum.
APPEND ls_final TO it_final.
CLEAR lv_sum.
ENDLOOP.
Regards
Dhananjay
2014 Mar 30 12:20 PM