select'2019-05-09' AS 业务日期,A.pcode AS 产品代码,sum(A.eamt) AS 放款金额,count(DISTINCT A.cust_no) AS 放款户数,count(1) AS 放款笔数,count(DISTINCT CASE WHEN B.con_no IS NULL THEN NULL ELSE A.cust_no END) AS 收回户数,sum(CASE WHEN B.con_no IS NULL THEN 0 ELSE 1 END) AS 收回笔数,sum(IFNULL(B.SXJE,0.00)) AS 收息金额,sum(IFNULL(B.DCJE,0.00)) AS 代偿金额,DATE(CURRENT_DATE()) AS 维护日期from loan_detail ALEFT JOIN(SELECT X.con_no,sum(X.r_amt-X.p_amt-X.p1_amt) AS LXSR,sum(IF(X.r_type='04' OR X.r_type='05',X.p1_amt+X.p1_amt,0.00)) AS DCJEFROM repay_detail XWHERE open_day<='2019-05-09' GROUP BY X.con_no ) as B ON A.con_no=B.con_nowhere A.open_day <='2019-05-09'GROUP BY A.p_code
select t.p_code as 产品码, CASE when t.amtT = '01' then '1000元以下' when t.amtT = '02' then '1000元-3000元' when t.amtT = '03' then '3000元-5000元' when t.amtT = '04' then '5000元-1万元' when t.amtT = '05' then '1万元-5万元' when t.amtT = '06' then '5万元-10万元' when t.amtT = '07' then '10万元-20万元' when t.amtT = '08' then '20万元-30万元' else '30万元以上' end as 维度, SUM(e_amt) as 发放金额, COUNT(DISTINCT cust_no) as 客户数 from (SELECT p_code, case when e_amt > 0 and e_amt <= 1000 then '01' when e_amt > 1000 and e_amt <= 3000 then '02' when e_amt > 3000 and e_amt <= 5000 then '03' when e_amt > 5000 and e_amt <= 1*10000 then '04' when e_amt > 1*10000 and e_amt <= 5*10000 then '05' when e_amt > 5*10000 and e_amt <= 10*10000 then '06' when e_amt > 10*10000 and e_amt <= 20*10000 then '07' when e_amt > 20*10000 and e_amt <= 30*10000 then '08' else '09' end as amtT, e_amt, cust_no FROM loan_detail AS X1 left join (select distinct loan_no from loan_detail where open_day <= '2019-05-09' and e_amt <0.00) AS X2 on X2.loan_no=X1.loan_no WHERE open_day <= '2019-05-09' and X2.loan_no is null ) t GROUP BY t.amtT,t.p_code