create view vi_overdue_contract2 as (
SELECT LRP.CONTRACT_ID                                               AS CONTRACT_ID,
       (sum(ifnull(LRP.CORPUS, 0)) - sum(ifnull(LRI.CORPUS, 0)))     AS YQ_CORPUS,
       (sum(ifnull(LRP.INTEREST, 0)) - sum(ifnull(LRI.INTEREST, 0))) AS YQ_INTEREST,
       round((sum(ifnull(`getRentPenalty`(LRP.PAYMENT_NUMBER, LRP.PLAN_LIST, date_format(now(), '%y/%m/%d')), 0)) - sum(ifnull(LRI.PENALTY, 0))),
             2)                                                      AS YQ_PENALTY,
       (to_days(now()) - to_days(min(LRP.PLAN_DATE)))                AS OVERDUE_DAY
FROM (APZL.LC_RENT_PLAN LRP
LEFT JOIN (
    SELECT APZL.LC_RENT_INCOME.CONTRACT_ID                AS CONTRACT_ID,
           APZL.LC_RENT_INCOME.PLAN_LIST                  AS PLAN_LIST,
           sum(APZL.LC_RENT_INCOME.RENT)                  AS RENT,
           sum(APZL.LC_RENT_INCOME.CORPUS)                AS CORPUS,
           sum(APZL.LC_RENT_INCOME.INTEREST)              AS INTEREST,
           sum(ifnull(APZL.LC_RENT_INCOME.PENALTY, 0.00)) AS PENALTY
    FROM APZL.LC_RENT_INCOME
    GROUP BY APZL.LC_RENT_INCOME.CONTRACT_ID, APZL.LC_RENT_INCOME.PLAN_LIST
) LRI ON (((LRI.CONTRACT_ID = LRP.CONTRACT_ID) AND (LRI.PLAN_LIST = LRP.PLAN_LIST))))
WHERE (((to_days(date_format(now(), '%Y/%m/%d')) - to_days(date_format(LRP.PLAN_DATE, '%Y/%m/%d'))) >= 3) AND
       ((LRP.CORPUS - ifnull(LRI.CORPUS, 0)) <> 0))
GROUP BY LRP.CONTRACT_ID);

-- comment on column vi_overdue_contract2.CONTRACT_ID not supported: 合同编号

