2020 Mar 23 11:29 AM
Hi, I have a sorted internal table which is sorted in descending order according to date (DATUV). While fetching data from table I am getting multiple duplicate records as dates are different. I want to keep only the latest record and remove the old duplicates. Please help me with logic ?
2020 Mar 23 11:37 AM
shashwat_b,
If i understood your issue right, you mean to say since there multiple records with same date your recent entry is not on the Top. Due to which you are not able to fetch the latest Record.
Below are few of my suggestions:
1) In addition to Date Field, check if there are any other fields that could help you in bring the latest entry on top. After that You can apply delete adjacent duplicates.
2) Also please use STABLE BY key word when you are sorting using a NON KEY Field. Below Code for reference.
SORT IT_TAB STABLE BY FIELD.
Also if you could let us know the complete scenario with the actual tables you are fetching from, we will be able to solve your issue better. Kindly help to post your question with enough details which will help in quicker resolution.
Regards!
2020 Mar 24 6:42 AM
I am updating the complete scenario. Please give your valuable inputs.
2020 Mar 23 1:05 PM
If the table is sorted, you can check this example:
DATA(itab) = VALUE tt_itab( FOR i = 1 THEN i + 1 UNTIL i > 100 ( date = sy-datum index = i ) ).
* Last entry:
DATA(lines) = lines( itab ).
DATA(last_entry) = itab[ lines ].
lines = lines - 1.
IF lines > 0.
DELETE itab TO lines
WHERE date = last_entry-date. "if you want to delete just lines with date found.
ENDIF.
Regards,
Andreas
2020 Mar 24 6:49 AM
I am updating the complete scenario. Please give your valuable inputs.
2020 Mar 24 6:48 AM
Here as you can see there are two IDNRK 1000012 and 1000013. For each IDNRK there are two dates. Here I only want the record with the latest date to be displayed not all the dates for each IDNRK.
Here is the code I am writing.
*Fetching Data from STKO and STPO Tables.
SELECT stko~stlty,
stko~stlnr,
stko~stlal,
stko~stkoz,
stko~bmein,
stko~bmeng,
stko~stlst,
stpo~stlkn,
stpo~stpoz,
stpo~datuv,
stpo~aennr,
stpo~idnrk,
stpo~posnr,
stpo~meins,
stpo~menge,
stpo~alpos,
stpo~ewahr,
stpo~clobk,
stpo~alpst,
stpo~alprf,
stpo~alpgr
FROM stko INNER JOIN stpo
ON stko~stlty = stpo~stlty
AND stko~stlnr = stpo~stlnr
INTO TABLE @DATA(lt_stpo)
FOR ALL ENTRIES IN @lt_mast_temp
WHERE stko~stlnr = @lt_mast_temp-stlnr
AND stko~stlal = @lt_mast_temp-stlal
AND stko~stlty IN @s_stlty
AND stko~stlst IN @s_stlst
AND stko~lkenz IN @s_lkenz
AND stpo~idnrk IN @s_idnrk
AND stpo~posnr IN @s_posnr.
IF sy-subrc = 0.
SORT lt_stpo BY stlnr stlal.
ENDIF.
ENDIF.
2020 Mar 24 7:27 AM
Hi
This is very old method, (may not be good - if you have very large data).. but to get your things, this will work.
Create some temp table to loop it - and store the latest recoed in the final.
it_stpo_temp[] = it_stpo[].
sort it_stpo_temp by idnrk datuv ascending.
sort it_stpo by idnrk datuv ascending.
move_flag = ''.
loop at it_stpo_temp into wa_stpo_temp.
"take the last record wich was sorted by datuv - hence max record will be picked
loop at it_stpo into wa_stpo where idnrk = wa_stpo_temp-idnrl.
move-corresponding wa_stpo_temp to wa_stpo_final.
move_flag = 'Y'.
endloop.
if move_flag = 'Y'.
append wa_stpo_final to it_stpo_final.
endif.
move_flag = ''.
endloop.
"it_stpo_final[] === is your final ittable
2020 Mar 24 8:20 AM
You only use IDNRK and DATUV, why don't you use STLTY and STLNR too? (key columns used to join STKO and STPO)