How to find the maximum value in a collection
769072Apr 3 2013 — edited Apr 3 2013Hi 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!