0

I need to flag the duplicate fields in a table but sy-tabix is always increasing. I expect for every knum group, a 1 in sy-tabix. Here is the code.

sort result_package stable by knumh zzklfn1.

loop at result_package assigning <result_fields> 
    group by ( knum = <result_fields>-knum ).
  if sy-tabix > 1.
    data(lv_duplicate) = 'x'.
  endif.
  lv_duplicate = ''.
endloop.

How to flag the duplicate record?

Tried to code but sy-tabix is increasing.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • thanks for your comment. Do you have any idea to flag for duplicate records – trapvader23 Apr 28 '23 at 13:55
  • The [ABAP documentation of `LOOP AT itab, GROUP BY`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abaploop_at_itab_group_by.htm) explains very well the rule why `sy-tabix` is set to 1 ("group key binding") but your code is using a "representative binding" so `sy-tabix` can get any value. Also, your question misses focus, I don't know if you are asking about how `sy-tabix` is set or how to "flag the duplicate" "fields" or "record" (it's unclear), and also your code always results in `lv_duplicate = ' '` so please focus more and clarify. – Sandra Rossi Apr 28 '23 at 17:38
  • Does this answer your question? [Extracting unique values from an internal table](https://stackoverflow.com/questions/32863043/extracting-unique-values-from-an-internal-table) – Suncatcher Aug 24 '23 at 07:42

1 Answers1

1

It depends for what you need this flag. You can do it simple: after sorting the table just loop over it and compare the current work area with the previous one and mark the duplicate flag if they are the same (or compare only the fields of interest).

If at the end you need the list without duplicates, after sorting you can use ADJACENT DUPLICATES keyword to remove duplicates:

DELETE ADJACENT DUPLICATES FROM result_package.

deletes all rows in certain groups of rows following one another, except for the first row of the group.

Additionally you can specify what to compare:

DELETE ADJACENT DUPLICATES FROM result_package [USING KEY keyname] | [COMPARING { comp1 comp2 ...} | {ALL FIELDS}]

If the addition COMPARING is not specified, the groups are determined by the content of the key fields of the table key being used. If no explicit table key is specified, the primary table key is used implicitly.

AlexSchell
  • 953
  • 1
  • 1
  • 18