DELIMITER $$

create ALGORITHM=UNDEFINED DEFINER=`apzl`@`%` SQL SECURITY DEFINER VIEW `vi_splitting_pay` AS (
SELECT
  `o`.`distributor_id`  AS `distributor_id`,
  `o`.`CONTRACT_NO`     AS `CONTRACT_NO`,
  `o`.`contract_id`     AS `contract_id`,
  `o`.`lccc_charge`     AS `lccc_charge`,
  `o`.`lccc_ratio`      AS `lccc_ratio`,
  `o`.`lccc_list`       AS `lccc_list`,
  `o`.`plan_list`       AS `plan_list`,
  `lul`.`CUSTOMER_NAME` AS `customer_name`,
  `lrp`.`interest_sp`   AS `sum_sp`,
  IFNULL(`lsp`.`splitting_money`,0) AS `splitting_money`,
  CAST((`lrp`.`interest_sp` * `o`.`lccc_ratio`) / 100 AS DECIMAL(22,2)) AS `should_sp`,
  CAST(((`lrp`.`interest_sp` * `o`.`lccc_ratio`) / 100) AS DECIMAL(22,2)) - IFNULL(`lsp`.`splitting_money`,0) AS `cur_sp`
FROM (((`apzl`.`vi_split_config` `o`
     LEFT JOIN (SELECT
                  SUM(`apzl`.`lc_rent_plan`.`INTEREST_sp`) AS `interest_sp`,
                  `apzl`.`lc_rent_plan`.`CONTRACT_ID` AS `contract_id`
                FROM `apzl`.`lc_rent_plan`
                WHERE (`apzl`.`lc_rent_plan`.`INTEREST_sp` IS NOT NULL)
                GROUP BY `apzl`.`lc_rent_plan`.`CONTRACT_ID`) `lrp`
       ON ((`lrp`.`contract_id` = `o`.`contract_id`)))
    LEFT JOIN (SELECT
                 SUM(IFNULL(`apzl`.`lc_splitting_pay`.`splitting_money`,0)) AS `splitting_money`,
                 `apzl`.`lc_splitting_pay`.`contract_id` AS `contract_id`,
                 MAX(`apzl`.`lc_splitting_pay`.`pay_list`) AS `pay_list`,
                 `apzl`.`lc_splitting_pay`.`id`          AS `id`
               FROM `apzl`.`lc_splitting_pay`
               GROUP BY `apzl`.`lc_splitting_pay`.`contract_id`) `lsp`
      ON (((`o`.`contract_id` = `lsp`.`contract_id`))))
   LEFT JOIN `apzl`.`lb_union_lessee` `lul`
     ON (((`lul`.`CONTRACT_ID` = `o`.`contract_id`)
          AND (`lul`.`IS_MAIN` = 'Y'))))
WHERE (IFNULL(lsp.pay_list,0)<>O.lccc_charge
       AND (`o`.`lccc_charge` > 0))

)$$

DELIMITER ;