Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

select distinct

Former Member
0 Likes
983

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.

7 REPLIES 7
Read only

JozsefSzikszai
Active Contributor
0 Likes
941

one possible way:

SORT itab.
DELETE ADJACENT DUPLICATES FROM itab.
distinct_values = lines( itab ).

Read only

Former Member
0 Likes
941

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

Read only

Former Member
0 Likes
941

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

Read only

Former Member
0 Likes
941

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.

Read only

Former Member
0 Likes
941
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.

Read only

Former Member
0 Likes
941

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

Read only

Former Member
0 Likes
941

asas