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: 

Retrieve distinct value from internal table

Former Member
0 Kudos

Hi expert anyone know how to retrieve distinct value of a column from a internal table?

5 REPLIES 5

abdul_hakim
Active Contributor
0 Kudos

Hi,

No you cannot.

Make the column as key fields for doing so!!

Cheers,

Hakim

mark all useful answers

Former Member
0 Kudos

Is possible! Try this:

Data: begin of itab occurs 0,

name(20) type c,

end of itab.

Data: name1(20) type c,

name2(20) type c.

Data: begin of itab1 occurs 0,

fullname(20) type c,

end of itab1.

Below are some record i had create in the internal table, note there are some repeated record for testing.

itab-name = 'peter'.

append itab.

itab-name = 'gary'.

append itab.

itab-name = 'john'.

append itab.

itab-name = 'john'.

append itab.

itab-name = 'peter'.

append itab.

itab-name = 'gary'.

append itab.

itab-name = 'ken'.

append itab.

sort itab.

loop at itab.

name1 = itab-name.

if name2 <> name1.

append itab1.

endif.

name2 = name1.

endloop.

Below itab1 is the distinct record for the itab. itab contain duplicated record. itab1 contain ONLY distinct record.

Loop at itab1.

write: / itab1-fullname.

endloop.

Reward point if any help

0 Kudos

Hey gary what is the value for name2?

What does the if statement do?

I don't quite understand the middle part of the coding.

0 Kudos

See the following ex:

types: begin of ty_itab1,

field1 type c,

field2 type c,

end of ty_itab1.

data: itab1 type standard table of ty_itab1 with header line,

itab2 type standard table of ty_itab1 with header line.

select * from

into table itab1.

itab2[] = itab1[].

sort itab1 by field1.

delete adjacent duplicates from itab1 comparing field1.

*******Now itab1 containes the distinct field1 values

0 Kudos

Hello Joce,

There are two types where we can solve this problem.

One deleting the duplicate entries in that internal table itself through the following code.

-


DATA: BEGIN OF it_bkpf OCCURS 0,

belnr LIKE bkpf-belnr,

END OF it_bkpf.

SELECT belnr INTO TABLE it_bkpf

FROM bkpf.

SORT it_bkpf BY belnr.

DELETE ADJACENT DUPLICATES FROM it_bkpf.

-


Or you can do one more thing if you dont want to delete the duplicate entries. Create one more internal table copy all the content of first internal table into the second and delete the duplicate entries in that.