Application Development 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: 

select distinct

Former Member
0 Kudos

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

JozsefSzikszai
Active Contributor
0 Kudos

one possible way:

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

former_member198275
Active Contributor
0 Kudos
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.

Former Member
0 Kudos

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

Former Member
0 Kudos

asas