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

please check my code...

aris_hidalgo
Contributor
0 Likes
1,184

Hello experts,

here is what my code does:

after appending to itab, it loops through itab and checks the first record of every instance of a new sernr. So when the sernr satisfies the IF conditon, it now move those records through another itab. the example below satisfies the condition since it's first record satisfies the IF.

sernr bwart

123456 702

123456 701

123456 711

but my problem is this, if it loops through another sernr where its first record does not satisfy the condition like the example below it must ignore the first record and the following records of the same sernr must be moved.

I edited my code and it works but here is the catch, when I use the same test data it NOW INSERTS THE FIRST RECORD WHEN IT SHOULDNT. Help would really be appreciated and comments too. Thanks you guys!

sernr bwart

654321 701 --> ignore/delete

654321 702 ---> move to itab

654321 701 ---> move to itab

here is my code btw.

data: l_sernr like t_ztm0019_tmp-sernr.

*get all records from table ztm0019 and append to itab.

SELECT * FROM ztm0019

APPENDING TABLE t_ztm0019.

*populate itab t_ztm0019_tmp.

t_ztm0019_tmp[] = t_ztm0019[].

*sort itab according to latest date and time grouped by serial no.

SORT t_ztm0019_tmp BY sernr datum DESCENDING uzeit DESCENDING.

SORT t_ztm0019 BY sernr datum DESCENDING uzeit DESCENDING.

LOOP AT t_ztm0019_tmp.

AT NEW sernr.

l_sernr = t_ztm0019_tmp-sernr.

READ TABLE t_ztm0019_tmp INDEX sy-tabix.

  • if condition is true, move to itab t_ztm_acc_variance.

IF t_ztm0019_tmp-bwart EQ '702'

OR t_ztm0019_tmp-bwart EQ '708'

OR t_ztm0019_tmp-bwart EQ '712'

OR t_ztm0019_tmp-bwart EQ '718'.

LOOP AT t_ztm0019 WHERE sernr = t_ztm0019_tmp-sernr

and bwart = t_ztm0019_tmp-bwart.

APPEND t_ztm0019 TO t_ztm_acc_variance.

CLEAR t_ztm0019.

DELETE t_ztm0019.

ENDLOOP.

*if condition is false, check for new sernr.

ELSE.

READ TABLE t_ztm0019_tmp INDEX sy-tabix.

delete t_ztm0019_tmp.

LOOP AT t_ztm0019_tmp.

IF t_ztm0019_tmp-sernr = l_sernr.

APPEND t_ztm0019_tmp TO t_ztm_acc_variance.

CLEAR t_ztm0019_tmp.

delete t_ztm0019_tmp.

ENDIF.

ENDLOOP.

ENDIF.

CONTINUE. " Go for next SERNR

ENDAT.

ENDLOOP.

*inserts or updates records in db table ztm_acc_variance based on itab

MODIFY ztm_acc_variance FROM TABLE t_ztm_acc_variance.

*insert or update unaccounted variances from itab

MODIFY ztm0019 FROM TABLE t_ztm0019.

*deletes records from db table ztm0019 based on matching entries in itab

DELETE ztm0019 FROM TABLE t_ztm_acc_variance.

1 ACCEPTED SOLUTION
Read only

vinod_gunaware2
Active Contributor
0 Likes
1,165

Hi

Use sort and then use DELETE ADJACENT DUPLICATES after sort. This will be helpful.

regards

vinod

8 REPLIES 8
Read only

vinod_gunaware2
Active Contributor
0 Likes
1,166

Hi

Use sort and then use DELETE ADJACENT DUPLICATES after sort. This will be helpful.

regards

vinod

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,165

Hi,

READ TABLE t_ztm0019_tmp INDEX sy-tabix.

  • if condition is true, move to itab t_ztm_acc_variance.

IF t_ztm0019_tmp-bwart EQ '702'

OR t_ztm0019_tmp-bwart EQ '708'

OR t_ztm0019_tmp-bwart EQ '712'

OR t_ztm0019_tmp-bwart EQ '718'.

LOOP AT t_ztm0019 WHERE sernr = t_ztm0019_tmp-sernr

and bwart = t_ztm0019_tmp-bwart.

APPEND t_ztm0019 TO t_ztm_acc_variance.

CLEAR t_ztm0019.

DELETE t_ztm0019.

ENDLOOP.

*if condition is false, check for new sernr.

ELSE.

<b>*No separate Read needded here.The foolowing will delete the current record if the condition not satisfied

delete t_ztm0019_tmp where sernr = t_ztm0019_tmp-sernr

bwart = t_ztm0019_tmp-bwart

uzeit = t_ztm0019-uzeit.</b>

LOOP AT t_ztm0019_tmp.

IF t_ztm0019_tmp-sernr = l_sernr.

APPEND t_ztm0019_tmp TO t_ztm_acc_variance.

CLEAR t_ztm0019_tmp.

delete t_ztm0019_tmp.

ENDIF.

ENDLOOP.

ENDIF.

Message was edited by: Jayanthi Jayaraman

Read only

0 Likes
1,165

ELSE.

READ TABLE t_ztm0019_tmp INDEX sy-tabix.

delete adjacent duplicate from t_ztm0019_tmp comparing sernr.

LOOP AT t_ztm0019_tmp.

IF t_ztm0019_tmp-sernr = l_sernr.

APPEND t_ztm0019_tmp TO t_ztm_acc_variance.

CLEAR t_ztm0019_tmp.

delete t_ztm0019_tmp.

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
1,165

hi viray,

if you want to do so..

after AT NEW sernr..

the bwart field wont be available...

SELECT * FROM ztm0019
APPENDING TABLE t_ztm0019.

*populate itab t_ztm0019_tmp.
t_ztm0019_tmp[] = t_ztm0019[].

*sort itab according to latest date and time grouped by serial no.
SORT t_ztm0019_tmp BY sernr datum DESCENDING uzeit DESCENDING.
SORT t_ztm0019 BY sernr datum DESCENDING uzeit DESCENDING.
data : cnt type i, flag type i.

LOOP AT t_ztm0019_tmp.


ON CHANGE OF sernr.
l_sernr = t_ztm0019_tmp-sernr.
IF t_ztm0019_tmp-bwart EQ '702'
 OR t_ztm0019_tmp-bwart EQ '708'
 OR t_ztm0019_tmp-bwart EQ '712'
 OR t_ztm0019_tmp-bwart EQ '718'.
flag = 1.
else.
 flag = 0.
endif.
ENDON.
*condition matches for the first record itself..
<b>if flag = 1.
*move the single record.
 MOVE t_ztm0019_tmp TO t_ztm_acc_variance.
 APPEND t_ztm_acc_variance.
 FLAG = 0.
else.
 loop at t_ztm0019 where sernr = t_ztm0019_tmp-sernr.
  if  cnt eq 1.
   move t_ztm0019 to t_ztm_acc_variance.
   append t_ztm_acc_variance.
  endif.
   cnt = 1.
  endif.
endloop.</b>
cnt = 0.
endloop.

then continue your logic..

this might work..

regards

satesh

Read only

andreas_mann3
Active Contributor
0 Likes
1,165

hi,

i've tried to simplify your prog:

LOOP AT t_ztm0019 ASSIGNING <f>.

  CLEAR del.

  AT NEW sernr.
    IF NOT <f>-bwart  IN s_bwart.
      del = 'X'.
    ENDIF.
  ENDAT.

  IF del = 'X'.
    DELETE  t_ztm0019 INDEX sy-tabix.
  ENDIF.

ENDLOOP.

Andreas

Read only

Former Member
0 Likes
1,165

HI viraylab

try this

data: l_sernr like t_ztm0019_tmp-sernr.

*get all records from table ztm0019 and append to itab.

SELECT * FROM ztm0019

APPENDING TABLE t_ztm0019.

*populate itab t_ztm0019_tmp.

t_ztm0019_tmp[] = t_ztm0019[].

*sort itab according to latest date and time grouped by serial no.

SORT t_ztm0019_tmp BY sernr datum DESCENDING uzeit DESCENDING.

SORT t_ztm0019 BY sernr datum DESCENDING uzeit DESCENDING.

LOOP AT t_ztm0019_tmp.

AT NEW sernr.

l_sernr = t_ztm0019_tmp-sernr.

IF t_ztm0019_tmp-bwart EQ '702'

OR t_ztm0019_tmp-bwart EQ '708'

OR t_ztm0019_tmp-bwart EQ '712'

OR t_ztm0019_tmp-bwart EQ '718'.

LOOP AT t_ztm0019 WHERE sernr = t_ztm0019_tmp-sernr

and bwart = t_ztm0019_tmp-bwart.

APPEND t_ztm0019 TO t_ztm_acc_variance.

CLEAR t_ztm0019.

DELETE t_ztm0019.

ENDLOOP.

ELSE.

delete t_ztm0019 where index = sy-tabix.

endif.

endat.

LOOP AT t_ztm0019 where sernr eq t_ztm0019_tmp-sernr and bwart eq t_ztm0019_tmp-bwart.

APPEND t_ztm0019 TO t_ztm_acc_variance.

CLEAR t_ztm0019.

delete t_ztm0019.

ENDLOOP.

ENDLOOP.

regards

kishore

Read only

Former Member
0 Likes
1,165

Hello Viraylab,

As u have indicated when second set of records come in as per ur logic it enter into else condition and deletes the first record. Then it moves the records into t_ztm_acc_variance table and deletes lines from t_ztm0019_tmp. However u also need to delete recordds from t_ztm0019 table as this is the source used for inserting records into ztm0019.

MODIFY ztm0019 FROM TABLE t_ztm0019.

Hope this helps.

Abhijit

Read only

0 Likes
1,165

HI Viray,

A small change in the previous code..

since i found it a little wrong.

hop this one works..

 
SELECT * FROM ztm0019 INTO TABLE t_ztm0019.

*populate itab t_ztm0019_tmp.
t_ztm0019_tmp[] = t_ztm0019[].

*sort itab according to latest date and time grouped by serial no.
SORT t_ztm0019_tmp BY sernr .
SORT t_ztm0019 BY sernr .
data : cnt type i, flag type i.

LOOP AT t_ztm0019_tmp.


ON CHANGE OF t_ztm0019_tmp-sernr.
*l_sernr = t_ztm0019_tmp-sernr.
IF t_ztm0019_tmp-bwart EQ '702'
 OR t_ztm0019_tmp-bwart EQ '708'
 OR t_ztm0019_tmp-bwart EQ '712'
 OR t_ztm0019_tmp-bwart EQ '718'.
flag = 1.
else.
 flag = 0.
endif.
ENDON.
*condition matches for the first record itself..
if flag = 1.
*move the single record.
 MOVE t_ztm0019_tmp TO t_ztm_acc_variance.
 APPEND t_ztm_acc_variance.
 FLAG = 0.
else.
 loop at t_ztm0019 where sernr = t_ztm0019_tmp-sernr.
  if  cnt eq 1.
   move t_ztm0019 to t_ztm_acc_variance.
   append t_ztm_acc_variance.
  endif.
   cnt = 1.
 endloop.
  endif.

cnt = 0.
endloop.

regards

satesh