Internal Tables Part 7: Removing Duplicates and Building Unique Datasets
Preview · a free account unlocks the full lesson
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 sectionsHide sections
Visual overview
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.
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.