DELIMITER $$

ALTER ALGORITHM=UNDEFINED DEFINER=`apzl`@`%` SQL SECURITY DEFINER VIEW `vi_normalsettle_contract` AS 
SELECT
  `lci`.`ID`              AS `contract_id`,
  `lci`.`CONTRACT_STATUS` AS `contract_status`,
  `lcc`.`INCOME_NUMBER`   AS `income_number`
FROM `apzl`.`lb_contract_info` `lci`
      LEFT JOIN (SELECT contract_id,SUM(rent) AS rent,MAX(PLAN_LIST) PLAN_LIST FROM `apzl`.`lc_rent_plan` GROUP BY contract_id)`lrp`
        ON `lrp`.`CONTRACT_ID` = `lci`.`ID`
     LEFT JOIN (SELECT contract_id,SUM(rent) AS rent,MAX(PLAN_LIST) PLAN_LIST FROM `apzl`.`lc_rent_income` GROUP BY contract_id) `lri`
       ON `lri`.`CONTRACT_ID` = `lci`.`id`
    LEFT JOIN `apzl`.`lc_contract_condition` `lcc`
      ON `lcc`.`CONTRACT_ID` = `lci`.`id`
   
WHERE 
       `lci`.`CONTRACT_STATUS` = '31' AND
 `lri`.`rent` -`lrp`.`RENT`>0
        AND `lrp`.`PLAN_LIST` = `lcc`.`INCOME_NUMBER`
        AND `lri`.`PLAN_LIST` = `lcc`.`INCOME_NUMBER`
$$

DELIMITER ;