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

problem with logic

Former Member
0 Likes
624

Hello all ,

iam new to abap.

i have a requirement where i need retain the fiest 3 records for the combination werks and matnr in an internal table and delete ramaining records for that combination

can any one pls help me how can i do this.

thanks

Ramesh

5 REPLIES 5
Read only

Former Member
0 Likes
601

Ramesh, there are many ways to achieve this.

sort table1 by werks matnr.

table2[] = table1[].

delete adj duplicates from table2 comparing werks matnr.

loop at table2 into wa2.

clear counter.

loop at table1 assigning <fs1> where werks = wa2-werks and matnr = wa-matnr.

counter = counter + 1.

if counter > 3.

clear <fs1>-werks.

endif.

endloop.

endloop.

delete table1 where werks is initial.

thanks,

Vikram.M

Read only

former_member295881
Contributor
0 Likes
601

Hi Ramesh.

Maybe try something like this

Declare a work area same as your internal table.

Data: wa_itab     type (same as your internal table),

      lv_tabix     type sy-tabix.

Loop at itab into wa_itab,

  lv_tabix = sy-tabix.

     if lv_tabix > 3.

       delete itab from wa_itab index lv_tabix.

     endif.

Endloop.

Read only

Former Member
0 Likes
601

Hi,

Try this for deleting first 3 rows.

while sy-tabix > 4.

  

   delete it where <condition>.

  

   endwhile. 

Read only

RaymondGiuseppi
Active Contributor
0 Likes
601

Another understanding of your requirement (seemingly not so clear  ?)

SORT itab BY matnr werks STABLE.

LOOP AT itab assiging <fs>.

  AT NEW werks.

    CLEAR counter.

  ENDAT.

  ADD 1 TO counter.

  IF counter GT 3

    DELETE itab.

  ENDIF.

ENDLOOP.

Regards,

Raymond

Read only

vigneshyeram
Active Participant
0 Likes
601

Hi Ramesh,

You can follow the logic given by . Its correct.

Thanks & Regards,

Vignesh Yeram