From time to time I hit this problem and so far I havn't found any nice solution. I've an internal table with several fields. I would like to get all unique values for one (or several) of these fields. However let say that this table has a lot of entries so making a copy is not an option. Also changing this table in any way is forbiden.
For example for table below I would like to get all unique values for field Number. In this case it would be 1,2,3,4.
Name | Number | name1 | 1| name2 | 2| name3 | 2| name4 | 3| name5 | 4| name5 | 3|
Can anyone propose me better solution than going in the loop through all entries in table? Maybe there is some ABAP functionality that I don't know about?
Heard about DELETE ADJACENT DUPLICATES ? Read the SAP documentation for further details
"Sort Internal Table based on the fields you want to delete the duplicate fields SORT ITAB BY NUMBER. DELETE ADJACENT DUPLICATES ROM ITAB COMPARING NUMBER.
Edited by: Suhas Saha on May 4, 2010 5:43 PM
Loop at the table and use event
AT NEW number
This will only stop at new numbers. This will work if the table is sorted. If your table isn't sorted, then you could start a paralel internal table containing the numbers that already appeared, and check againts it to see if it already appeared, this table could be of sorted type. A final tip, if you use AT NEW remeber to close it with an ENDAT.
1. Yes I heard about DELETE ADJACENT DUPLICATES, but I also wrote that table shouldn't be changed in any way. So this is not good solution.
2. No, this table is not sorted. You can also see this in example data.
Never less thank you for your answers. Anybody have other ideas?
Going more into details is rather pointless and would take a lot of time to explain background. Just as I said I can't modify this table in any way and I want all unique values that occured in one of fields. Let's say that if I sort this table I won't be able to restore it to previous order which is important for me.
Thank you Mishra for answer but as I wrote in first message I would like to have nicer solution than going through all entries in loop.
Well, with the restriction of copy and modification on original internal table you got no choice but to loop through each record. I would suggest loop through records using field symbol and use COLLECT statement (with some consideration) to get unique records.
Let's say that if I sort this table I won't be able to restore it to previous order which is important for me
True...If you sort the table you won't be able to restore. So the only option is to copy/move all the records into another table.
Sorting If you need to retrive unique values. I don't think without sorting the table would be a nice idea and proper programming to proceed ahead.
Regarding logic, as replied earlier
Either we can go with DELETE ADJACENT DUPLICATES or proceed as replied in my earlier post. There might be number of algorithms to resolve this. But we cannot go ahead without sorting or looping.
Maybe there is some ABAP functionality that I don't know about
If DELETE ADJACENT DUPLICATES is not the case. You can code something like this:
Loop your Internal table. Use READ TABLE statement to fetch the first number and store it in a variable.
Again for the second time the it reads the second and compare it with the first one stored. If found equal delete or else continue.
You can use DELETE ADJACENT DUPLICATES
[Delete adjacent duplicates-SAP Help|http://help.sap.com/saphelp_nw04/helpdata/en/06/aafd54fc4011d195280000e8353423/content.html]
If you can't change the original table in any way and can't duplicate it, the problem just can't be solved. Because you need to sort an internal table to apply any algorithm which would look up unique values, or, if you can't sort, you need to have an extra table to keep unique entries which would be found.
Best regards, Eugene.