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: 

Problem in loop

Former Member
0 Kudos

Hai Friends,

The following is my Internal Table

A	B	S1	S2	S3
101	1	45		
101	1	45		
101	1	45		
101	1	45		
101	2		80	
101	2		80	
101	3			150
101	3			150

But i want as below,

A	S1	S2	S3
			
101	45	80	150

If B is 1 the value should be moved to S1

If B is 2 the value should be moved to S2

If B is 3 the value should be moved to S3.

1 ACCEPTED SOLUTION

Sm1tje
Active Contributor
0 Kudos

create another internal table with all fields except column B.

First sort your itab by the first column and delete all duplicates. Delete adjacent duplicates....

Now loop at your original itab.

Loop at itab...

if column S1 is not initial.

move S1 to second itab column S1.

elseif column S2.

elseif column S3.

endif.

endif.

endloop.

3 REPLIES 3

Sm1tje
Active Contributor
0 Kudos

create another internal table with all fields except column B.

First sort your itab by the first column and delete all duplicates. Delete adjacent duplicates....

Now loop at your original itab.

Loop at itab...

if column S1 is not initial.

move S1 to second itab column S1.

elseif column S2.

elseif column S3.

endif.

endif.

endloop.

former_member188685
Active Contributor
0 Kudos

sort the internal table with A and B, Delete the duplicates based on A and B.

have the internal table with the fields what ever you want in the final table.

now loop the table

loop at it_data.
it_final-a = it_data-a.
wa_data  = it_data.
at new b.
 if not wa_data-s1 is initial.
   it_final-s1 = wa_data-s1.
elseif not wa_data-s2 is initial.
  it_final-s2 = wa_data-s2.
elseif not wa_data-s3 is initial.
 it_final-s3 = wa_data-s3.
endif.
endat.
at end of a.
 append it_final.
clear it_final.
endat.

endloop.

Former Member
0 Kudos

Hi Vennila,

Try the following code.

loop at itab1.

on change of itab1-a.

append itab2.

clear itab2.

endon.

itab2-a = itab1-a.

if itab1-b = 1.

itab2-s1 = itab1-s1.

elseif itab1-b = 2.

itab2-s2 = itab1-s2.

elseif itab1-b = 3.

itab2-s3 = itab1-s3.

endif.

endloop.

you will result in itab2.