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: 

Sample code to pick up a MAX value from an internal table column?

Former Member
0 Kudos

We know that we can use Select MAX statement to pick up a a max value from a system table, but we are not sure if we can do the same thing for internal table. We've got an internal table itab which looks like:

A------B

x1-----3

x2-----1

x3-----7

x4-----2

We'd be appreciated if some ABAP expert here can show us the sample code to pick up the max value of column B which is 7 from the above internal table itab!

<REMOVED BY MODERATOR>

Edited by: Alvaro Tejada Galindo on Apr 10, 2008 12:56 PM

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

You can do as below:

sort itab by B descending.

READ TABLE itab INDEX 1.

Thanks,

Sriram Ponna.

9 REPLIES 9

Former Member
0 Kudos

Hi,

You can do as below:

sort itab by B descending.

READ TABLE itab INDEX 1.

Thanks,

Sriram Ponna.

0 Kudos

hi Sriram Ponna,

Could you complete your code on how to pick up the max value? e.g., Write the max value?

Thanks!

0 Kudos

Try this way


data : v_max_value type i.
sort itab by B descending.
read table itab index 1.
if sy-subrc eq 0.
   move itab-b to v_max_value.
endif.

0 Kudos

Hi,

Please refer the code below:


 data : BEGIN OF itab OCCURS 0,
        a(2) type c,
        b type i,
      end of itab.

itab-a = 'x1'.
itab-b = 3.
append itab.
CLEAr itab.

itab-a = 'x2'.
itab-b = 1.
append itab.
CLEAr itab.

itab-a = 'x3'.
itab-b = 7.
append itab.
CLEAr itab.

itab-a = 'x4'.
itab-b = 2.
append itab.
CLEAr itab.

sort itab by b DESCENDING.

read table itab index 1.

write : itab-a, itab-b. 

Thanks,

Sriram Ponna.

former_member583013
Active Contributor
0 Kudos

SORT T_TABLE BY FIELD_B DESCENDING.

READ TABLE T_TABLE INDEX 1.

Just sorted from MAX to MIN and read the first record -;)

Greetings,

Blag.

0 Kudos

hi Alvaro Tejada Galindo

Could you complete your code on how to pick up the max value?

Thanks!

Former Member
0 Kudos

Hi Kevin,

try the following code.

types:begin of it,

a type i,

b type i,

end of it.

data:itab type standard table of it.

data:wa_itab type it.

wa_itab-a = 3. wa_itab-b = 4.

append wa_itab to itab.

wa_itab-a = 3. wa_itab-b = 6.

append wa_itab to itab.

wa_itab-a = 3. wa_itab-b = 2.

append wa_itab to itab.

wa_itab-a = 3. wa_itab-b = 1.

append wa_itab to itab.

sort itab by b descending.

read table itab into wa_itab index 1.

write: wa_itab-b.

<REMOVED BY MODERATOR>

Edited by: Alvaro Tejada Galindo on Apr 10, 2008 1:50 PM

Former Member
0 Kudos

hi Sriram Ponna/Alvaro Tejada G.../a®s/krishna chaitan... ,

Your suggestions by using

READ TABLE itab2 into gs_itab1 INDEX 1.

causes the conflict with our another statement in the end of itab1 loop:

Modify itab1 from gs_itab1 index sy-tabix.

that the above Modify statement always modify itab1 at 1st row and also causes an unlimited loop that the program is hung up. Our code looks as the following:?


Loop AT itab1 into gs_itab1.
         .......
         use Select statement to fill in itab2...
         .......

         SORT itab2 BY f DESCENDING. "f is a field of itab2
         READ TABLE itab2 into gs_itab1 INDEX 1.
         Modify itab1 from gs_itab1 index sy-tabix.

EndLoop.

The above simple code is for each itab1 line, create a new itab2 by using select statement, and then grab the max value of field f in itab2 and fill it into a corresponding field in itab1. However the last two lines of statements in the itab1 loop causes the program hang!

Any solution?

Thanks in advance!

former_member197561
Active Participant
0 Kudos

There is way to get the line which has the maximum value of field with at least 2 times less CPU time.
Instead of using

  SORT  SORT < _tableA> BY < x > DESCENDING. 
READ <_tableA > INTO < lineA > INDEX 1. 
rather use
READ TABLE  <_tableA> INTO < lineB > INDEX 1. (we get the first line)
LOOP AT < tableA > into < lineA >.
IF lineB-x < lineA-x. (we compare if there is line with bigger value than the first line and if yes, assign).
  lineB = lineA.
ENDIF.

In this way, in case the table is not used sorted further, we make only 1 loop of comparison, and you can imagine to sort the entire table it makes a lot of more loops for comparison.