Application Development and Automation 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: 
Read only

Compare records

Former Member
0 Likes
691

Hi,

This is very urgent, i need to compare two records of an internal table and then add one counter to it.

like

Filed1 Field2

A 100

A 200

B 300

C 400

Now i have to set a counter for field1 and for A the value of the counter would be 2. For B and C it would be 1. Can you suggest how to do that? It is really urgent.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
662

Internal table itab has following data:

Field1 Field2

A 100

A 200

B 300

C 400

data: counter type i.

sort itab by field1 field2.

loop at itab.

at new field1.

clear counter.

endat

counter = counter + 1.

at end of field1.

move itab-field1 to itab1-field1.

move counter to itab1-counter.

append itab1.

endat.

endloop.

itab1 will now contain

A 2

B 1

C 1

Reward if useful.

Internal table itab has following data:

Field1 Field2

A 100

A 200

B 300

C 400

data: counter type i.

sort itab by field1 field2.

loop at itab.

at new field1.

clear counter.

endat

counter = counter + 1.

at end of field1.

move itab-field1 to itab1-field1.

move counter to itab1-counter.

append itab1.

endat.

endloop.

itab1 will now contain

A 2

B 1

C 1

Reward if useful.

5 REPLIES 5
Read only

Sm1tje
Active Contributor
0 Likes
662

loop at internal table.

if field1 = 'A'.

add 1 to counter_a.

elseif field1 = 'B'.

add to counter_b.

elseif field1 = 'C'.

add 1 to counter_c.

endloop.

Read only

Former Member
0 Likes
662

Hi,

There is not only 3 values , there is a around 1000 values, i cant hardcode all of them, i need to compare between the two adjacent rows, i need a logic for that.

Read only

Former Member
0 Likes
662

With the DELETE ADJACENT DUPLICATES FROM itab comparing A or B. you will have all distinct value.

I suggest that you keep your internal table as counter where you will put the value counter next of the original value.

Read only

Former Member
0 Likes
663

Internal table itab has following data:

Field1 Field2

A 100

A 200

B 300

C 400

data: counter type i.

sort itab by field1 field2.

loop at itab.

at new field1.

clear counter.

endat

counter = counter + 1.

at end of field1.

move itab-field1 to itab1-field1.

move counter to itab1-counter.

append itab1.

endat.

endloop.

itab1 will now contain

A 2

B 1

C 1

Reward if useful.

Read only

Former Member
0 Likes
662

hi,

You must already have a counter by different value that you have in the table or you know what king of value you will have?

you can sort you table by A and use the event AT :

AT - itab

Syntax

LOOP AT itab result ...

[AT FIRST.

...

ENDAT.]

[AT NEW comp1.

...

ENDAT.

[AT NEW comp2.

...

ENDAT.

[...]]]

[ ... ]

[[[...]

AT END OF comp2.

...

ENDAT.]

AT END OF comp1.

...

ENDAT.]

[AT LAST.

...

ENDAT.]

ENDLOOP.

and then do the same thing with sorting by B.

Regards