04-10-2008 5:51 PM
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
04-10-2008 5:57 PM
Hi,
You can do as below:
sort itab by B descending.
READ TABLE itab INDEX 1.
Thanks,
Sriram Ponna.
04-10-2008 5:57 PM
Hi,
You can do as below:
sort itab by B descending.
READ TABLE itab INDEX 1.
Thanks,
Sriram Ponna.
04-10-2008 6:35 PM
hi Sriram Ponna,
Could you complete your code on how to pick up the max value? e.g., Write the max value?
Thanks!
04-10-2008 6:41 PM
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.
a®
04-10-2008 6:43 PM
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.
04-10-2008 5:58 PM
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.
04-10-2008 6:34 PM
hi Alvaro Tejada Galindo
Could you complete your code on how to pick up the max value?
Thanks!
04-10-2008 6:43 PM
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
04-11-2008 12:19 PM
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!
08-06-2014 1:38 PM
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.