10-13-2008 9:46 AM
Dear experts,
I have an internal table called i_maintable.
i_maintable has records assuming
5168
5168
5168
5172
5172
5172
How to count distinct elements (most optimally):
Ex in this case 2.
10-13-2008 9:50 AM
one possible way:
SORT itab.
DELETE ADJACENT DUPLICATES FROM itab.
distinct_values = lines( itab ).
10-13-2008 9:51 AM
Another method.
DATA count TYPE i.
sort itab by ebeln.
LOOP AT itab.
at NEW ebeln.
count = count + 1.
endat.
ENDLOOP.
WRITE count.
Regards
Sathar
10-13-2008 9:52 AM
Hi,
Loop at i_maintable into wa_maintable.
at new field1.
count = count + 1.
endat.
endloop.
or else while extracting from the data base table use select query to get the count.
for Eg: Select Distinct (field1) count.............
With Regards,
Dwaraka.S
10-13-2008 9:55 AM
Hi,
Take care of a few things :
1. Make sure that your itab is sorted in ascending order of the key.
2. Then loop at itab, compare the current key value with the previous key value and keep incremementing until they differ.
3. Remember to use field symbol instead of work area.
regards,
Advait.
10-13-2008 9:56 AM
SORT itab by number.
DELETE ADJACENT DUPLICATES FROM itab comparing number.
data l1 type i.
describe TABLE itab lines l1.
now l1 will be 2.
10-13-2008 10:08 AM
Hi Aditya,
You can use the following code snippet to count the distinct elements:
DATA: l_index TYPE sy-tabix.
SORT i_maintable BY <fieldname>.
DELETE ADJACENT DUPLICATES FROM i_maintable.
LOOP AT i_maintable INTO wa_maintable.
l_index = sy-tabix.
ENDLOOP.
after this loop, you can use the value of l_index as the number of distinct elements in your i_maintable.
To use the above code snippet this you need to make a work area also for i_maintable.
Hope it will help you,
Regards,
Nikita
11-03-2008 9:02 AM