05-06-2009 12:14 PM
Hello Experts !
I have been through various threads in the ABAP forum, however, couldn't find any solution for my query.
I need to find the maximum and minimum values from an internal table.
e.g.
Particulars Col1 Col2 Col3 Col4 Col5
A 4 8 2 9 7
B 1 5 6 3 4
C 8 2 3 4 7
-
Min 1 2 2 3 4
-
Max 8 8 6 9 7
-
Can anyone give me a few suggestions on this. How do I need to proceed?
Thanks,
Aasim
05-06-2009 12:24 PM
>
> Hello Experts !
>
> I have been through various threads in the ABAP forum, however, couldn't find any solution for my query.
> I need to find the maximum and minimum values from an internal table.
>
> e.g.
>
> Particulars Col1 Col2 Col3 Col4 Col5
> A 4 8 2 9 7
> B 1 5 6 3 4
> C 8 2 3 4 7
> -
> Min 1 2 2 3 4
> -
> Max 8 8 6 9 7
> -
>
>
> Can anyone give me a few suggestions on this. How do I need to proceed?
>
> Thanks,
> Aasim
below pseudo code should work
data : wa_max type workarea ,
wa_min type workarea ,
wa_tmp type workarea .
loop at internal_table into wa_tmp.
if sy-tabix = 1 ,
wa_max = wa_tmp.
wa_min = wa_tmp.
else.
* minimum
if wa_tmp-Col1 < wa_min-Col1 .
wa_min-Col1 = wa_tmp-Col1 .
endif.
* repeat above IF.ENDIF for each col
* Maximum
if wa_tmp-Col1 > wa_max-Col1 .
wa_max-Col1 = wa_tmp-Col1 .
endif.
* repeat above IF.ENDIF for each col
endif.
endloop.
05-06-2009 12:19 PM
05-06-2009 12:21 PM
Sort the table individually for each field and the get the maximum for each in some variable and the clooect the same in workarea of the internal table.
Edited by: harsh bhalla on May 6, 2009 4:51 PM
05-06-2009 12:24 PM
>
> Hello Experts !
>
> I have been through various threads in the ABAP forum, however, couldn't find any solution for my query.
> I need to find the maximum and minimum values from an internal table.
>
> e.g.
>
> Particulars Col1 Col2 Col3 Col4 Col5
> A 4 8 2 9 7
> B 1 5 6 3 4
> C 8 2 3 4 7
> -
> Min 1 2 2 3 4
> -
> Max 8 8 6 9 7
> -
>
>
> Can anyone give me a few suggestions on this. How do I need to proceed?
>
> Thanks,
> Aasim
below pseudo code should work
data : wa_max type workarea ,
wa_min type workarea ,
wa_tmp type workarea .
loop at internal_table into wa_tmp.
if sy-tabix = 1 ,
wa_max = wa_tmp.
wa_min = wa_tmp.
else.
* minimum
if wa_tmp-Col1 < wa_min-Col1 .
wa_min-Col1 = wa_tmp-Col1 .
endif.
* repeat above IF.ENDIF for each col
* Maximum
if wa_tmp-Col1 > wa_max-Col1 .
wa_max-Col1 = wa_tmp-Col1 .
endif.
* repeat above IF.ENDIF for each col
endif.
endloop.
05-06-2009 1:08 PM
Hey Pawan !
That was BULLSEYE. Exactly what I needed. Thanks a TON pal. I really appreciate it
Cheerz !
Aasim
03-04-2010 11:21 AM
Hi,
Can you try this?
describe table itab lines lines.
no_cols = 5.
do no_cols times.
wa_sort = sy-index.
concatenate 'col' wa_sort into wa_sort.
sort itab by (wa_sort).
read table itab index 1 into wa1 transporting (wa_sort).
read table itab index lines into wa2 transporting (wa_sort).
enddo.
write : / 'Min Value:' .
write wa1.
write : / 'Max Value:' .
write wa2.
Thanks,
Uma
05-06-2009 12:26 PM
Hi Aasim,
first sort the internal table on which field you want the Maximum and minimum..
example..
sort itab by field1 decending.
next ..
read itab index 1. "in the first field the Minimum value will be there.
if sy-subrc = 0.
"now move the table filed and display the value display is manimum
endif.
describe lines of itab into w_count. "it count total number of lines
in itab
read itab index w_count. " in the last field the Maximum value will be there
if sy-subrc = 0.
"now move the table filed and display the value display is maximum
endif.
now by reading the first and last field you will get the minimum and maximum value
regards,
Prabhudas
09-04-2013 3:02 PM
we can do dis by sorting itab and read itab by index
for max value -> sort itab DESCENDING order and read itab where index = 1.
for min value -> sort itab ASCENDING order and read itab where index = 1.
simply and no need of loop
02-20-2014 4:20 PM
Hi Suresh,
Is there an easy way to return the table to the previous order after you do this?
Thanks.
KR,
Bruno
02-20-2014 6:45 PM
Hi Bruno Esperanca.
u can achieve this ,just copy itab data to any other temp itab ,get min / max by obove method and clear itab again move the data in temp itab to original itab
02-20-2014 8:30 PM
Hi Suresh,
Thanks for your reply. What I was hoping was for something "cool" like READ TABLE itab WHERE MAX(field).
But I guess we'll never have something like this...
Thanks!
Bruno
02-21-2014 5:08 AM
Hi Bruno
You do realize the thread is a bit old.
I find the old school correct answer to be cooler than sort/read combination. Sorting entire table just to get a max/min value feels like overkill. However, sort/read has better readability.
To make it cooler, you can create a class method that does it dynamically.
max_order = zcl_itab=>max( itab , 'ORDER' ).
02-21-2014 7:04 AM
02-24-2014 4:49 PM