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: 

Comparing records within an internal table

former_member1326872
Participant
0 Kudos
79

Hi,

I have to compare records within an ITAB.

eg

ITAB

Field1 Field2 Field3 Field4

Abc 06 Xyz 100

Def 05 Xyz 25

I have to compare the records withing this ITAB

If field1 & 3 are same in two records and field2 is different in the same two records, then I have to subract the fourth fields ..

Please let me know how can I code this? I'm confused..

Thanks..

Uma.

Message was edited by:

Uma

1 ACCEPTED SOLUTION

Former Member
0 Kudos
58

hi try this out:

1. Declare one more internal table itab2 and populate it with all the records that r present in itab1.

2. LOOP AT itab1 INTO wa1.

LOOP AT itab2 INTO wa2.

if wa1-field1 = wa2-field1 and

wa1-field3 = wa2-field3 and

wa1-field2 ne wa2-field2.

-


then do ur calculation here----

ENDLOOP.

ENDLOOP.

Please reward pts if useful!!

4 REPLIES 4

Former Member
0 Kudos
58

loop at itab.

if itab-field1 = itab-field3.

if itab-fld1 ne itab-fld2 and itab-fld3 ne itab-fld2.

use your subtract condition.

endif.

endif.

endloop.

Former Member
0 Kudos
58

I think you have to make a copy of ITAB then loop through itab and read itab1 where itab1-field3 = itab-field1.

Rob

Former Member
0 Kudos
59

hi try this out:

1. Declare one more internal table itab2 and populate it with all the records that r present in itab1.

2. LOOP AT itab1 INTO wa1.

LOOP AT itab2 INTO wa2.

if wa1-field1 = wa2-field1 and

wa1-field3 = wa2-field3 and

wa1-field2 ne wa2-field2.

-


then do ur calculation here----

ENDLOOP.

ENDLOOP.

Please reward pts if useful!!

Former Member
0 Kudos
58

hi

try following code !!!

data : begin of sample occurs 0,

f1(2),

f2(2),

f3(2),

f4(2),

end of sample,

wa_sample like sample,

final_sample like sample occurs 0.

sample-f1 = 'a'.

sample-f2 = 'b'.

sample-f3 = '1'.

sample-f4 = '12'.

append sample.

sample-f1 = 'a'.

sample-f2 = 'b'.

sample-f3 = '1'.

sample-f4 = '13'.

append sample.

sample-f1 = 'a'.

sample-f2 = 'c'.

sample-f3 = '1'.

sample-f4 = '14'.

append sample.

read table sample index 1 into wa_sample.

loop at sample.

read table sample comparing f1 f3.

if sy-subrc EQ 0.

if sample-f2 NE wa_sample-f2.

<<< subtraction coding>>>

append sample to final_sample.

endif.

endif.

endloop.

rewars points if u find it correct as per ur requirements

Nilesh