比较两个日期并计算日期差

aiqt4smr  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(392)

我必须比较以下表格:
时间表:

正式收据表:

我只想通过比较正式收据和时间表之间的日期,知道客户是否按照他的时间表付款。如果没有,它将给他10美元的罚款,每天计算天数从预定日期。
示例:第一个付款计划是2019-11-02。但手术室显示他在2019年12月10日付了钱。比他的第一次付款计划晚了38天。然后处罚。你知道吗?谢谢您。
我想要这样的东西:

Loanid     | PaymentSched | Date OR    | Past Due | Penalty
H1807.0008 | 2019-11-02   | 2019-12-10 | 38 Days  | 380
92dk7w1h

92dk7w1h1#

假设没有遗漏付款和部分付款,那么一个选项是用 row_number() ,然后将它们连接在一起。剩下的只是过滤逾期付款,计算逾期天数和罚款:

select 
    s.loan_id,
    s.date_payment,
    r.date_or,
    datediff(day, s.date_payment, r.date_or) as past_due_days,
    10 * datediff(day, s.date_payment, r.date_or) as penalty
from (
    select s.*, row_number() over(partition by loan_id order by date_payment) rn
    from schedule s
    where total_payment > 0
) s
inner join (
    select r.*, row_number() over(partition by loan_id order by date_or) rn
    from official_receipt r
) r on s.loan_id = r.loan_id and s.rn = r.rn and s.total_payment = r.amount
where r.date_or > s.date_payment
kx5bkwkv

kx5bkwkv2#

datediff会帮你的

select datediff(day,'2019-10-02','2019-12-10')

select [Loanid],[PaymentSched],[Date OR],datediff(day,[PaymentSched],[Date OR]) as [Past Due],datediff(day,[PaymentSched],[Date OR])*10 as Penalty
from Schedule s
join [Official receipt] o on o.[Loanid]=s.[Loanid]

相关问题