Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to find the maximum value in a collection

769072Apr 3 2013 — edited Apr 3 2013
Hi All,

I'm using Oracle 11gR2. I need to find out the maximum value in a collection of elements.

When I execute the following, I'm getting maximum # of collection as '5' but I need the max value itself which is 8, Please assist.


DECLARE
type v_number1 is table of number index by binary_integer;

v_number v_number1;

idx NUMBER;
psrc_file VARCHAR2(10) := '1,2,4,8,6';
v_src_file_string_array v_number1;
max_temp number;

BEGIN

SELECT regexp_substr(psrc_file, '[^,]+', 1, level) coll
BULK COLLECT INTO v_src_file_string_array
FROM (SELECT psrc_file FROM dual)
CONNECT BY LEVEL <= length(regexp_replace(psrc_file, '[^,]+')) + 1;

-- Set up test values:
FOR i IN v_src_file_string_array.FIRST .. v_src_file_string_array.COUNT LOOP
--v_numbers(i) := v_src_file_string_array(i);
v_number (i) := i;
END LOOP;

max_temp := v_number (v_number.first);
idx := v_number.first;

-- Display array contents:
while idx is not null
loop
if v_number (idx) >= max_temp
then
max_temp := v_number (idx);
end if;

idx := v_number.next (idx);
end loop;

dbms_output.put_line ('Max: ' || max_temp);
end;
/


Thanks!
This post has been answered by jeneesh on Apr 3 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 1 2013
Added on Apr 3 2013
7 comments
1,592 views