0

I need to flag the duplicate values and I found a solution using the code below:

sort result_package stable by knumh zzklfn1.
data lv_prev_knumh type c length 10.

loop at result_package assigning <result_fields>.
  if <result_fields>-knumh <> lv_prev_knumh.
    lv_prev_knumh = <result_fields>-knumh.
    <result_fields>-zzstafkz = ''.
  else.
    <result_fields>-zzstafkz = 'X'.
  endif.
endloop.

But obviously the first duplicated value is not flagged. Output is shown below:

knumh zzstafkz
001
002
002 x
002 x
003

I don't know really what should I do next.

AlexSchell
  • 953
  • 1
  • 1
  • 18
  • 2
    Why do you need to find duplicates, what do you want to do with them? If you mark all the same records as duplicates it would be logically the same as no records are marked at all, and is totally meaningless. Please clarify your target. – AlexSchell Jun 13 '23 at 13:53
  • 1
    Your question doesn't seem to be tightly coupled to SAP BW, it just seems an issue with algorithm (and then possibly an ABAP question): if you detect that a line has same `KNUMH` as previous line, you should set `ZZSTAFKZ` = 'X' on the previous line too. Simple as that. If you don't know how to update a line at a given index, please ask it. See [how to ask](https://stackoverflow.com/help/how-to-ask). – Sandra Rossi Jun 13 '23 at 15:55

1 Answers1

1

You can simply do it by keeping the reference to the previous record and assign X flag when the condition meets.

Types declaration and sample data for test:

TYPES: BEGIN OF ty_result_package,
         knumh    TYPE knumh,
         zzstafkz TYPE flag,
       END OF ty_result_package,
       ty_t_result_package TYPE STANDARD TABLE OF ty_result_package
                           WITH DEFAULT KEY.

DATA(lt_result_package) = VALUE ty_t_result_package(
    ( knumh = '001'  )
    ( knumh = '002'  ) ( knumh = '002'  ) ( knumh = '002'  )
    ( knumh = '003'  )
    ( knumh = '004'  ) ( knumh = '004'  ) ).

The logic itself:

FIELD-SYMBOLS: <fs_result_package_prev> type ty_result_package.

SORT lt_result_package BY knumh ASCENDING.
LOOP AT lt_result_package ASSIGNING FIELD-SYMBOL(<fs_result_package>).
  IF <fs_result_package_prev> IS ASSIGNED AND
     <fs_result_package>-knumh = <fs_result_package_prev>-knumh.

    <fs_result_package_prev>-zzstafkz = <fs_result_package>-zzstafkz = 'X'.
  ELSE.
    CLEAR <fs_result_package>-zzstafkz.  " clear the flag if necessary
  ENDIF.

  ASSIGN <fs_result_package> TO <fs_result_package_prev>.
ENDLOOP.

Sample output:

cl_demo_output=>display_data( EXPORTING value = lt_result_package ).

lt_result_package

AlexSchell
  • 953
  • 1
  • 1
  • 18