create function getRemainCorpus(p_payment_number varchar(40), p_plan_list int) returns decimal(18,2)
RETURN (
 select case when p_plan_list=1 then lcc.clean_lease_money else round(max(ifnull(lcc.clean_lease_money,0))-sum(ifnull(lrp.corpus,0)),2) end from lc_rent_plan lrp 
 left join lc_calc_condition lcc on lrp.payment_number=lcc.payment_number 
 where lrp.payment_number=p_payment_number and (case when p_plan_list=1 then lrp.plan_list=p_plan_list else lrp.plan_list<p_plan_list end) group by lrp.payment_number
);

