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: 

moving last 2 records on change of material Number in a sorted table

mawasey03
Participant
0 Kudos

Dear Gurus,

i have an internal table with material no,Qty and Posting dt.

i have sorted it based on material and date.

I want to move last two records from this itab to other itab on change of each material.

Example

1 ACCEPTED SOLUTION

former_member223133
Active Participant
0 Kudos

Hi Abdul,

Below code will help you.

FIELD-SYMBOLS: <fs> TYPE wa1.

LOOP AT itab1 ASSIGNING <fs>.
   AT END OF material.
     lv_index = sy-tabix - 1.
     READ TABLE itab1 INTO wa1 INDEX lv_index.
     IF sy-subrc = 0.
       APPEND wa1 TO itab2.
     ENDIF.
     APPEND <fs> TO itab2.
   ENDAT.
ENDLOOP.


Regards

Gangadhar



4 REPLIES 4

shaik_sajid
Active Contributor
0 Kudos

Hi,

Use the following algorithm (Check Syntax).

Loop at itab1 into wa1.

append wa1 to it_dummy.

at new material.

refresh it_dummy.

append wa1 to it_dummy.

at end of material.

describe table it_dummy lines n.

if n le 3.

append  it_dummy to it2.

elseif n gt 3.

lv_n = n - 3.

delete it_dummy from 1 to lv_n.

endif.

append  it_dummy to it2.

endloop.

Regards

Sajid

0 Kudos

Dear Sajid

This code is not fetching first 3 records to other table.

regards

Abdul

former_member223133
Active Participant
0 Kudos

Hi Abdul,

Below code will help you.

FIELD-SYMBOLS: <fs> TYPE wa1.

LOOP AT itab1 ASSIGNING <fs>.
   AT END OF material.
     lv_index = sy-tabix - 1.
     READ TABLE itab1 INTO wa1 INDEX lv_index.
     IF sy-subrc = 0.
       APPEND wa1 TO itab2.
     ENDIF.
     APPEND <fs> TO itab2.
   ENDAT.
ENDLOOP.


Regards

Gangadhar



mayur_priyan
Active Participant
0 Kudos

TYpes: Begin of it1,

         one  TYPE string,

         two TYPE i,

         three TYPE sy-datum,

        END OF it1,

        tt_it1 TYPE STANDARD TABLE OF it1.

DATA: it_1 TYPE tt_it1,

       it_2 TYPE tt_it1,

       wa1 TYPE it1,

       wa_2 TYPE it1.

DATA : lv_index TYPE sy-index.

FIELD-SYMBOLS: <fs> TYPE it1.

              wa1-one   = 'A'.

              wa1-two   = 24.

              wa1-three = '20140304'.

              APPEND wa1 to it_1.

              wa1-one   = 'A'.

              wa1-two   = 25.

              wa1-three = '20140305'.

              APPEND wa1 to it_1.

              wa1-one   = 'A'.

              wa1-two   = 13.

              wa1-three = '20140306'.

              APPEND wa1 to it_1.

              wa1-one   = 'B'.

              wa1-two   = 12.

              wa1-three = '20140307'.

              APPEND wa1 to it_1.

              wa1-one   = 'B'.

              wa1-two   = 25.

              wa1-three = '20140308'.

              APPEND wa1 to it_1.

              wa1-one   = 'B'.

              wa1-two   = 3.

              wa1-three = '20140309'.

              APPEND wa1 to it_1.

UNASSIGN: <fs>.

LOOP AT it_1 ASSIGNING <fs>.

    AT END OF one.

      lv_index = sy-tabix - 1.

      READ TABLE it_1 INTO wa1 INDEX lv_index.

      IF sy-subrc = 0.

        APPEND wa1 TO it_2.

      ENDIF.

      APPEND <fs> TO it_2.

    ENDAT.

ENDLOOP.

UNASSIGN: <fs>.

LOOP AT it_2 ASSIGNING <fs>.

   WRITE: / <fs>-one,   <fs>-two,   <fs>-three.

ENDLOOP.