# 2016.3.31 的 账龄在贷---全部提取未还款----执行的结果是7222562.8
set @viewday = '2016-03-31';
select
  timestampdiff(
    month,
    concat(
      date_format(p.loan_time, '%Y-%m'),
      '-01'
    ),
    concat(
      date_format(
        if(
          p.supposed_repay_date > @viewday,
          p.supposed_repay_date,
          @viewday
        ),
        '%Y-%m'
      ),
      '-01'
    )
  ) loan_age,
  date_format(p.supposed_repay_date, "%Y-%m"),
  sum(p.supposed_principle_rule) principle
from
  qunaer.repayment_plan_20160331 p
  left join
    (select
      product_no,
      current_stage_no,
      real_repay_principle
    from
      qunaer.repay_refund_split_deyu
    where current_stage_no > 0
      and date(real_repay_date) <= @viewday) r
    on p.`product_no` = r.`product_no`
    and p.`current_stage_no` = r.`current_stage_no`
where r.real_repay_principle is null #OR p.supposed_principle_rule-r.real_repay_principle>0
group by 1,
  2
having principle > 0 ;

# 2016.6.30 的 账龄在贷---全部提取未还款----执行的结果是23703562.92
set @viewday = '2016-06-30';
select
  timestampdiff(
    month,
    concat(
      date_format(p.loan_time, '%Y-%m'),
      '-01'
    ),
    concat(
      date_format(
        if(
          p.supposed_repay_date > @viewday,
          p.supposed_repay_date,
          @viewday
        ),
        '%Y-%m'
      ),
      '-01'
    )
  ) loan_age,
  date_format(p.supposed_repay_date, "%Y-%m"),
  sum(p.supposed_principle_rule) principle
from
  qunaer.repayment_plan_20160630 p
  left join
    (select
      product_no,
      current_stage_no,
      real_repay_principle
    from
      qunaer.repay_refund_split_deyu
    where current_stage_no > 0
      and date(real_repay_date) <= @viewday) r
    on p.`product_no` = r.`product_no`
    and p.`current_stage_no` = r.`current_stage_no`
where r.real_repay_principle is null #OR p.supposed_principle_rule-r.real_repay_principle>0
group by 1,
  2
having principle > 0 ;

# 2016.9.30 的 账龄在贷---全部提取未还款----执行的结果是59938799.86
set @viewday = '2016-09-30';
select
  timestampdiff(
    month,
    concat(
      date_format(p.loan_time, '%Y-%m'),
      '-01'
    ),
    concat(
      date_format(
        if(
          p.supposed_repay_date > @viewday,
          p.supposed_repay_date,
          @viewday
        ),
        '%Y-%m'
      ),
      '-01'
    )
  ) loan_age,
  date_format(p.supposed_repay_date, "%Y-%m"),
  sum(p.supposed_principle_rule) principle
from
  qunaer.repayment_plan_20160930 p
  left join
    (select
      product_no,
      current_stage_no,
      real_repay_principle
    from
      qunaer.repay_refund_split_deyu
    where current_stage_no > 0
      and date(real_repay_date) <= @viewday) r
    on p.`product_no` = r.`product_no`
    and p.`current_stage_no` = r.`current_stage_no`
where r.real_repay_principle is null #OR p.supposed_principle_rule-r.real_repay_principle>0
group by 1,
  2
having principle > 0 ;



# 截止到 2016.12.31 的 账龄在贷---全部提取未还款----执行的结果是 82044284.22
SET @viewday='2016-12-31';
SELECT TIMESTAMPDIFF(MONTH,CONCAT(DATE_FORMAT(pay.trans_time,'%Y-%m'),'-01'),CONCAT(DATE_FORMAT(IF(DATE(plan.deadline)>=@viewday,plan.deadline,@viewday),'%Y-%m'),'-01')) 账龄,
DATE_FORMAT(plan.deadline,"%Y-%m") 应还月,SUM(plan.principle) 在贷金额
FROM qunaer_new.repayment_plan_2016 plan
JOIN qunaer_new.pay_detail pay ON plan.product_no= pay.product_no
WHERE plan.repay_no IS NULL AND pay.loan_time<=@viewday
GROUP BY 1,2

# 截止到 2017.3.31 的 在贷金额---全部提取未还款----执行结果是 97207498.26
SET  @viewday='2017-03-31';
SELECT loan_age,month1,SUM(amount) FROM
(SELECT TIMESTAMPDIFF(MONTH,CONCAT(DATE_FORMAT(pay.trans_time,'%Y-%m'),'-01'),CONCAT(DATE_FORMAT(IF(DATE(plan.deadline)>=@viewday,plan.deadline,@viewday),'%Y-%m'),'-01')) loan_age,
DATE_FORMAT(plan.deadline,"%Y-%m") month1,SUM(plan.principle) amount
FROM `repayment_plan_2016_201703` plan
JOIN pay_detail pay ON plan.product_no= pay.product_no
WHERE DATE(pay.`loan_time`) <= @viewday
GROUP BY 1,2
UNION ALL
SELECT TIMESTAMPDIFF(MONTH,CONCAT(DATE_FORMAT(pay.trans_time,'%Y-%m'),'-01'),CONCAT(DATE_FORMAT(IF(DATE(plan.deadline)>=@viewday,plan.deadline,@viewday),'%Y-%m'),'-01')) loan_age,
DATE_FORMAT(plan.deadline,"%Y-%m") month1,SUM(plan.principle) amount
FROM `repayment_plan_201703` plan
JOIN pay_detail pay ON plan.product_no= pay.product_no
WHERE DATE(pay.loan_time) <= @viewday AND plan.status != 3
GROUP BY 1,2
UNION ALL
SELECT TIMESTAMPDIFF(MONTH,CONCAT(DATE_FORMAT(pay.trans_time,'%Y-%m'),'-01'),CONCAT(DATE_FORMAT(IF(DATE(plan.deadline)>=@viewday,plan.deadline,@viewday),'%Y-%m'),'-01')) loan_age,
DATE_FORMAT(plan.deadline,"%Y-%m") month1,-SUM(repay.repay_principle) amount
FROM `repayment_plan_2016_201703` plan
JOIN pay_detail pay ON plan.product_no= pay.product_no
AND DATE(pay.`loan_time`) <= @viewday
JOIN `repay_detail` repay ON plan.`product_no` = repay.`product_no` AND plan.`current_stage_no` = repay.`current_stage_no` AND DATE(repay.`repay_time`) <= @viewday
GROUP BY 1,2
UNION
SELECT TIMESTAMPDIFF(MONTH,CONCAT(DATE_FORMAT(pay.trans_time,'%Y-%m'),'-01'),CONCAT(DATE_FORMAT(IF(DATE(plan.deadline)>=@viewday,plan.deadline,@viewday),'%Y-%m'),'-01')) loan_age,
DATE_FORMAT(plan.deadline,"%Y-%m") month1,-SUM(repay.repay_principle) amount
FROM `repayment_plan_2017` plan
JOIN pay_detail pay ON plan.product_no= pay.product_no
AND DATE(pay.`loan_time`) <= @viewday
JOIN `repay_detail` repay ON plan.`product_no` = repay.`product_no` AND plan.`current_stage_no` = repay.`current_stage_no` AND DATE(repay.`repay_time`) <= @viewday
GROUP BY 1,2) as a
GROUP BY 1,2

# 截止到 2017.3.31 的 在贷金额---全部提取未还款----执行结果是 136594833.06

SELECT SUM(t1.principle) 在贷本金 FROM( SELECT SUM(plan.principle) principle FROM qunaer_new.repayment_plan_2016_201703 plan JOIN qunaer_new.pay_detail pay ON plan.product_no=pay.product_no
WHERE plan.`status`=1 AND pay.trans_time<'2017-04-01'
UNION  all
SELECT SUM(plan.principle) principle FROM qunaer_new.repayment_plan_201703 plan JOIN qunaer_new.pay_detail pay ON plan.product_no=pay.product_no WHERE plan.`status`=1 AND pay.trans_time<'2017-04-01' )t1;

# 截止到 2017.6.30 的 在贷金额---全部提取未还款----执行结果是 136271830.15
