I want to find out min and max value on different where condition. I want to find out max value only when min value is null.
Remember lc_cont_yymm is variable and isMaxRequired, pc_cont_no, ld_Today are parameters in the function. Table Employees - 2125244 rows. primary Index on - Cont_no, cont_yymm
SELECT MIN(cont_yymm) INTO lc_cont_yymm
FROM Employees
WHERE cont_no = pc_cont_no
AND ld_Today BETWEEN eff_date AND term_date;
IF lc_cont_yymm IS NULL THEN
IF isMaxRequired THEN
SELECT MAX(cont_yymm) INTO lc_cont_yymm
FROM Employees
WHERE cont_no = pc_cont_no
AND ld_Today > term_date;
IF lc_cont_yymm IS NULL THEN
RETURN '-2';
END IF;
ELSE
RETURN '-2';
END IF;
END IF;
RETURN lc_cont_yymm;
This query taking much time when call repeatedly in for..loops. Even more time, when min value is null and try to find the max value. I want to optimize in better way. Please help on this?