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: 

Regarding Max/Min from an internal table

aasim_khan
Participant
0 Kudos

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

1 ACCEPTED SOLUTION

Pawan_Kesari
Active Contributor
0 Kudos

>

> 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.

13 REPLIES 13

Former Member
0 Kudos

Hi

sort the internal table and get the values

0 Kudos

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

Pawan_Kesari
Active Contributor
0 Kudos

>

> 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.

0 Kudos

Hey Pawan !

That was BULLSEYE. Exactly what I needed. Thanks a TON pal. I really appreciate it

Cheerz !

Aasim

0 Kudos

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

Former Member
0 Kudos

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

Former Member

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

0 Kudos

Hi Suresh,

Is there an easy way to return the table to the previous order after you do this?

Thanks.

KR,

Bruno

0 Kudos

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

0 Kudos

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

0 Kudos

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' ).

0 Kudos

I will

Thanks,

Bruno

0 Kudos

Hi Manish,

. Try it out and let me know what you think.

Thanks.

Best regards,

Bruno