历史数据回溯

BEGIN
-- 按日回退还款明细数据,start_date格式(2019-05-01)
declare start_date date;
set @start_date = daydate;
  -- drop table if EXISTS repayment_detail_day;
  -- create table repayment_detail_day as
insert into repayment_detail_day
select a.*,
       case when  date(当前计划还款日) >= @start_date then 0
            when  date(还款日期) is null then 1
            when  date(还款日期) is not null and  date(还款日期)<= date(当前计划还款日) then 0
            when  date(还款日期) is not null and  date(还款日期)> date(当前计划还款日) and @start_date> date(还款日期) then 0
            when  date(还款日期) is not null and  date(还款日期)> date(当前计划还款日) and @start_date<= date(还款日期) then  1
        end as 当前是否逾期,
       case when  date(当前计划还款日) >= @start_date then 0
            when  date(还款日期) is null then to_days(@start_date)-to_days( date(当前计划还款日))
            when  date(还款日期) is not null and  date(还款日期)<= date(当前计划还款日) then 0
            when  date(还款日期) is not null and  date(还款日期)> date(当前计划还款日) and @start_date> date(还款日期) then to_days( date(还款日期))-to_days( date(当前计划还款日))
            when  date(还款日期) is not null and  date(还款日期)> date(当前计划还款日) and @start_date<= date(还款日期) then to_days(@start_date)-to_days( date(当前计划还款日))
       end as 付清时或当前逾期天数,
       @start_date 当前日期
from (
      select c.contract_id 合同ID,
             c.customer_num 客户编号,
             d.repaying_plan_detail_id 还款计划明细ID,
             d.current_period 当前期数,
             d.current_end_date 当前计划还款日,
             d.current_principal 当前应还本金,
             d.pay_loan_date 还款日期,
             d.repayed_principal 已还本金
       from repaying_plan c
       join repaying_plan_detail d 
	     on c.repaying_plan_id=d.repaying_plan_id
	   where pay_loan_date < @start_date
      order by contract_id,repaying_plan_id,current_period ) a
order by 合同ID,当前期数;
END
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值