Hi Jacques, thanks for your suggestions.
I am not "having trouble" with this easy problem. It's clear and easy to solve.
The point is, as I stated: a simple problem should have a simple solution.
I am just looking for a simpler solution than those that I presented. Here, "simple" means: few but expressive code lines, no distractions, no noisy extras but only statements that directly expose their purpose.
- Unfortunately, the CDS proposal is off-topic. The problem is about internal tables, not database tables.
- The goal of a secondary index is to make the accesses to the internal table more efficient. My question was not about accessing the source table LT_VBAK, but about extracting a small peace of information from it; the distinct values that a certain column attains. Also, if you try to write a peace of code using your secondary index idea, you will see that you reach at one or the other of the code snippets above. There is no trick to get the secondary index itself directly accessible in ABAP (like "get key ... into table lt_ernam" or so). A secondary index only makes table row accesses more efficient, but table access with key is not the point here (no access with key here: it's clear that we have to loop over all entries in some way or the other to get our result)
- Your "classical approach" is the second of my examples (using an auxiliary hash), which of course also works with a hash table instead of a sorted table.
The MOVE-CORRESPONDING is not necessary, since in the classical approach one has to loop over the table anyway, so one can simply move the <LS_VBAK>-ERNAM to the target work area, which is not a structure but a simple field of type ERNAM.
Also, the DELETE ADJACENT DUPLICATES can be avoided by making the auxiliary table unique key. The "INSERT <LS_VBAK>-ERNAM INTO TABLE LT_ERNAM" will do nothing if the entry for this ERNAM already exists. Thus, the table will contain only the distinct ERNAM values when the loop is finished. No cleanup with DELETE ADJACENT DUPLICATES necessary.
Best regards,
Rüdiger