Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Compare 2 rows of an internal table

Former Member
0 Kudos

Hello,

I have a requirement where I have to compare the rows of an internal table (table containing poduction order no., material no, required quantity and used quantity) and get a count of the required quantity and used quantity for the same materials belonging to one production order.

For E.g.:

Prod Ord Material Req. Qty Used Qty

123 A 1 1

133 A 2 0

123 B 1 1

123 A 3 3

The quantities of Rows 1 and 4 need to be summed up. (as they correspond to the same material used within the same production order).

Please let me know if there is a method to do this.

I tried using SUM within AT END OF...ENDAT, but it does not do the summation, maybe because these quantity fields have the data type as QUAN.

Helpful suggestions will be greatly appreciated.

Thanks,

Rugmani

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Try using this:

1. Use the COLLECT command when u populate the internal table. Use it instead of Append.

ex: COLLECT WA_COMP TO IT_COMP.

2. Create another itab: IT_COMP2.

LOOP AT IT_COMP INTO WA_COMP.

COLLECT WA_COMP TO IT_COMP2.

ENDLOOP.

COLLECT compares all the character fields and sums the numeric and amount fields if the character fields are same.

See if it works...

- Hemant

7 REPLIES 7

martin_voros
Active Contributor
0 Kudos

Hi,

have you tried to use COLLECT. Check COLLECT in ABAP documentation.

Cheers

Former Member
0 Kudos

Hi,

Before you should sort the field.

sort <field>

then

at end of <filed>

sum

endat.

0 Kudos

Hello,

I tried using the AT END OF...ENDAT but it doesnt work.

My code is like this:

SORT mat_comp_list BY aufnr matnr.

LOOP AT mat_comp_list INTO wa_comp_list.

AT END OF aufnr.

AT END OF matnr.

SUM.

MODIFY mat_comp_list FROM wa_comp_list.

ENDAT.

ENDAT.

ENDLOOP.

Please let me know if this is correct.

Thanks,

Rugmani

Former Member
0 Kudos

Hi,

Try this..

SORT mat_comp_list BY aufnr matnr.
DATA: wa_comp_list_tmp LIKE wa_comp_list.
LOOP AT mat_comp_list INTO wa_comp_list. 

* Store the work area.
  wa_comp_list_tmp = wa_comp_list.

AT END OF matnr. 
SUM. 
* Move the quantity.
wa_comp_list_tmp-required_qty = wa_comp_list-required_qty.
wa_comp_list_tmp-used_qty = wa_comp_list-used_qty.

MODIFY mat_comp_list FROM wa_comp_list_tmp. 
ENDAT. 
ENDLOOP

.

Thanks

Naren

Former Member
0 Kudos

Hi,

Try using this:

1. Use the COLLECT command when u populate the internal table. Use it instead of Append.

ex: COLLECT WA_COMP TO IT_COMP.

2. Create another itab: IT_COMP2.

LOOP AT IT_COMP INTO WA_COMP.

COLLECT WA_COMP TO IT_COMP2.

ENDLOOP.

COLLECT compares all the character fields and sums the numeric and amount fields if the character fields are same.

See if it works...

- Hemant

0 Kudos

Thanks, that solved my problem.

Former Member
0 Kudos

Hi,

Check the following code:

DATA: BEGIN OF LINE,

COL1 TYPE C,

COL2 TYPE I,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE

WITH UNIQUE KEY COL1 COL2.

LINE-COL1 = 'A'.

DO 3 TIMES.

LINE-COL2 = SY-INDEX.

LINE-COL3 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 'B'.

DO 3 TIMES.

LINE-COL2 = 2 * SY-INDEX.

LINE-COL3 = ( 2 * SY-INDEX ) ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

SORT ITAB.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

AT END OF COL1.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

SKIP.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDAT.

ENDLOOP.

Regards,

Bhaskar