ERPClimb logoERPClimb
Internal Tables
ABAP Developmentintermediate 8 min read

Internal Tables Part 7: Removing Duplicates and Building Unique Datasets

Preview · a free account unlocks the full lesson

Key takeaway

Remove duplicate rows reliably: SORT + DELETE ADJACENT DUPLICATES with COMPARING, unique-key tables, collecting unique keys for FOR ALL ENTRIES, and the modern GROUP BY alternative.

In this lessonShow sections

Visual overview

Internal Tables Part 7: Removing Duplicates and Building Unique Datasets — learning sheetIn one line: Remove duplicate rows reliably: SORT + DELETE ADJACENT DUPLICATES with COMPARING, unique-key tables, collecting unique keys for FOR ALL ENTRIES, and the modern GROUP BY alternative.ERPClimb.com / ABAP / LESSON 7/10 / LEARNING SHEETInternal Tables Part 7: Removing Duplicates and BuildingUnique Datasets1 · IN ONE LINERemove duplicate rows reliably: SORT + DELETE ADJACENT DUPLICATES with COMPARING, unique-keytables, collecting unique keys for FOR ALL ENTRIES, and the modern GROUP BY alternative.2 · KEY IDEAS EXPLAINEDThe Classic PatternRows are compared with their predecessor; onlyadjacent equal rows are removed.Without SORT (or with a different sort order),duplicates remain.Building A Unique Key List (For All…Copy only the key field into a small table (LOOP +APPEND or VALUE with FOR).SORT + DELETE ADJACENT DUPLICATES.Unique TablesDATA gt_cust TYPE SORTED TABLE OF ty_cust WITHUNIQUE KEY customid.INSERT … INTO TABLE — duplicates are rejected(sy-subrc = 4), no error.Side Note: Group By (7.40+)LOOP AT gt_book INTO DATA(ls) GROUP BY ls-customidINTO DATA(lv_cust).3 · DO · AVOIDBest practice!Common mistakeSORT by exactly the COMPARING fields directlybefore DELETE ADJACENT DUPLICATES.Use unique sorted/hashed tables when buildinglists incrementally.De-duplicate and check non-empty before FORALL ENTRIES.✕DELETE ADJACENT DUPLICATES without sortingfirst.✕Sorting by different fields than COMPARING.✕APPEND of duplicates into unique sorted tables(runtime error).4 · IN A REAL PROJECTA report used DELETE ADJACENT DUPLICATES on a customer list that had been sorted by city, not by customernumber. The output still contained duplicates, and the follow-up FOR ALL ENTRIES read the same customers manytimes — the SQL trace showed 12,000 selects for 3,000 customers. Sorting by the compared field before deleting…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 6 covered changing tables. A very common task after that is getting a unique list out of a table — unique airlines, unique customers — either for output or as input to a following SELECT.

The classic pattern is SORT gt_book BY carrid connid. followed by DELETE ADJACENT DUPLICATES FROM gt_book COMPARING carrid connid.

This works because DELETE ADJACENT DUPLICATES only removes duplicates that are next to each other — it compares each row only with its immediate predecessor, so the table has to be sorted by the same fields first, or duplicates that aren't adjacent will survive.

COMPARING defines which fields make a row a duplicate: in the example above it's carrid and connid; leaving it off and using COMPARING ALL FIELDS instead only removes rows that are completely identical in every field.

Building a unique key list to drive a FOR ALL ENTRIES read follows a similar shape. To read customer master data for 10,000 bookings, for instance, you'd first extract just the customer numbers into a small table — either with a LOOP and APPEND, or with a VALUE constructor using FOR — then SORT that small table and run DELETE ADJACENT DUPLICATES on it, and finally check it isn't empty before using it to drive SELECT … FOR ALL ENTRIES IN, as covered in the Open SQL topic.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.