cancel
Showing results for 
Search instead for 
Did you mean: 

Summing fields in an Internal table

Former Member
0 Kudos

Hello,

I have an internal table in the following format:

Prod Order Material Req Qty Used Qty

A1 M1 2 2

A1 M2 1 0

A2 M3 3 3

A3 M1 4 0

I need to find out the material usage i.e. the no. of production orders the material was used in along with the sum of the required qty and used qty.

In the example above, the results will be

Material Req Qty Used Qty PO count

M1 6 2 2

M2 1 0 1

M3 3 3 1

I tried the following code:

SORT mat_comp_list_tmp BY matnr.

LOOP AT mat_comp_list_tmp INTO wa_comp_list_tmp.

  • Store the work area.

wa_comp_list_final = wa_comp_list_tmp.

AT END OF matnr.

SUM.

  • Move the quantity.

wa_comp_list_final-req_qty = wa_comp_list_tmp-req_qty.

wa_comp_list_final-used_qty = wa_comp_list_tmp-used_qty.

wa_comp_list_final-cnt_aufnr = sy-dbcnt.

MODIFY mat_comp_list_tmp FROM wa_comp_list_final.

ENDAT.

ENDLOOP.

But this gives me a PO count per material per Prod. Order. This is not desired.

Is there any other way to acheive this. Any help will be greatly appreciated.

Thanks,

Rugmani

Accepted Solutions (1)

Accepted Solutions (1)

david_carballido
Active Participant
0 Kudos

Hi .. I try this.

DATA: l_cont TYPE i,

itab TYPE mat_comp_list_tmp OCCURS 0 WITH HEADER LINE.

SORT mat_comp_list_tmp BY matnr.

LOOP AT mat_comp_list_tmp INTO wa_comp_list_tmp.

AT NET matnr.

CLEAR: l_cont, itab.

ENDAT.

ADD 1 TO l_cont.

ADD wa_comp_list_tmp-req_qty TO itab-req_qty.

ADD wa_comp_list_tmp-used_qty TO itab-used_qty.

AT END OF matnr.

itab_cnt_aufnr = l_cont.

APPEND itab.

ENDAT.

ENDLOOP.

PD: itab must have matnr as first field

Thanks and Regards.

David Carballido

Former Member
0 Kudos

Thank you, that solved my problem.

Answers (1)

Answers (1)

Former Member
0 Kudos

The AT END OF command is called each time any field up to and including the specified field changes. So it's called on every line with your current example. If you change the internal table to place material first it will work.

Production number first

Material Req Qty Used Qty PO count

M1 2 2 1

M2 1 0 1

M3 3 3 1

M1 4 0 1

Material number first

M1 6 2 2

M2 1 0 1

M3 3 3 1