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: 

3rd largest value in internal table

Former Member
0 Kudos

How to find the third largest value in internal table.

please give me logic without using sort statement?

and how to find the 3rd largest value with using sort on internal table.

please give the both logic separately?

<REMOVED BY MODERATOR>

Edited by: Alvaro Tejada Galindo on Apr 21, 2008 5:28 PM

3 REPLIES 3

Former Member
0 Kudos

>SORT TABLE DESCENDING BY Value.

>DELETE ADJACENT DUPLICATES COMPARING value.

>READ table INDEX 3 INTO wa.*

Other way would be to loop over table after sort.

loop at table into wa.

at new value.

add 1 to counter.

if counter = 3.

  • here is 3rd largets value.

endif.

endat.

endloop.

Former Member
0 Kudos

First sort the internal table. and read the 3 record by index.

if duplicate exists use delete adjacent duplicates and read the 3 record.

second method.

loop at itab into wa_itab.

if sy-tabix = 1.

lv_high = wa_itab-value.

else.

if lv_high > wa_itab-value.

else.

lv_high = wa_itab-value.

endif.

endif.

endloop.

loop at itab into wa_itab < lv_high.

if sy-tabix = 1.

lv_sechigh = wa_itab-vlaue.

else.

if lv_shigh > wa_itab-value.

else.

lv_sechigh = wa_itab-value.

endif.

endif.

endloop.

loop at itab into wa_itab < lv_sechigh.

if sy-tabix = 1.

lv_thirdhigh = wa_itab-vlaue.

else.

if lv_thirdhigh > wa_itab-value.

else.

lv_thirdhigh = wa_itab-value.

endif.

endif.

endloop.

Former Member
0 Kudos

Hello Sanjeev-

For retrieving 3rd largest value.

1st you need to sort the itab.

2nd read with index 3.

sort itab.

read table itab into workarea index 3.

Cheers,

~Srini....