create view vi_distributor_lines as (
SELECT DI.DISTRIBUTOR_NO                                                                                                                           AS DISTRIBUTOR_NO,
       DI.TOTAL                                                                                                                                    AS TOTAL,
       sum(ifnull(LCC.CLEAN_LEASE_MONEY, 0))                                                                                                       AS USEDLINES,
       (CASE
            WHEN ((DI.TOTAL = '') OR (DI.TOTAL = NULL)) THEN ''
            ELSE round((ifnull(DI.TOTAL, 0) - sum(ifnull(LCC.CLEAN_LEASE_MONEY, 0))), 2) END)                                                      AS RELINES
FROM ((((APZL.DISTRIBUTOR_INFO DI LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCI.DISTRIBUTOR_ID = DI.DISTRIBUTOR_NO))) LEFT JOIN APZL.LC_CALC_CONDITION LCC ON ((LCI.ID = LCC.CONTRACT_ID))) LEFT JOIN (
    SELECT max(APZL.LB_FILE_ARCHIVING_INFO.MORTGAGE_COMPLETION_TIME) AS MORTGAGE_COMPLETION_TIME,
           APZL.LB_FILE_ARCHIVING_INFO.CONTRACT_ID                   AS CONTRACT_ID
    FROM APZL.LB_FILE_ARCHIVING_INFO
    GROUP BY APZL.LB_FILE_ARCHIVING_INFO.CONTRACT_ID
) LFAI ON ((LFAI.CONTRACT_ID = LCI.ID)))
LEFT JOIN APZL.PRD_SPECIFIC_LIBRARY PSL ON (((PSL.BUSINESSTYPE = convert(LCI.PRODUCT_ID USING UTF8)) AND (PSL.EQUIP_TYPE = 'new_car'))))
WHERE ((LCI.CONTRACT_STATUS = 31) AND (isnull(LFAI.MORTGAGE_COMPLETION_TIME) OR (LFAI.MORTGAGE_COMPLETION_TIME = '')))
GROUP BY DI.DISTRIBUTOR_NO);

-- comment on column vi_distributor_lines.distributor_no not supported: 经销商编码

-- comment on column vi_distributor_lines.total not supported: 额度

