DELIMITER $$

ALTER ALGORITHM=UNDEFINED DEFINER=`apzl`@`%` SQL SECURITY DEFINER VIEW `apzl`.`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
                  `apzl`.`lc_rent_plan`.`CONTRACT_ID` AS `contract_id`,
                  SUM(`apzl`.`lc_rent_plan`.`RENT`)   AS `rent`,
                  MAX(`apzl`.`lc_rent_plan`.`PLAN_LIST`) AS `PLAN_LIST`
                FROM `apzl`.`lc_rent_plan`
                GROUP BY `apzl`.`lc_rent_plan`.`CONTRACT_ID`) `lrp`
       ON ((`lrp`.`contract_id` = `lci`.`ID`)))
    LEFT JOIN (SELECT
                 `apzl`.`lc_rent_income`.`CONTRACT_ID` AS `contract_id`,
                 SUM(`apzl`.`lc_rent_income`.`RENT`)   AS `rent`,
                 MAX(`apzl`.`lc_rent_income`.`PLAN_LIST`) AS `PLAN_LIST`
               FROM `apzl`.`lc_rent_income`
               GROUP BY `apzl`.`lc_rent_income`.`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 ;