V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
xcc7624
V2EX  ›  MySQL

SQL 查询逾期还款问题

  •  
  •   xcc7624 · 2016-12-24 17:17:16 +08:00 via Android · 3801 次点击
    这是一个创建于 2672 天前的主题,其中的信息可能已经有所发展或是发生改变。
    各位求指导
    现在我们把借款的还款计划设计在 Loan 表中,合同号 ID ,还款期次 term ,应还款日期 planpay ,实际还款日 actually ;现在现在希望当天首次出现逾期超过 30 天的合同号和逾期天数。如何处理
    3 条回复    2017-02-14 10:07:17 +08:00
    oclock
        1
    oclock  
       2016-12-24 19:16:20 +08:00
    看起来 id 和(term, planpay, actually)有一对多关系

    select
    id, MAX(age(coalesce(actually, current_timestamp), planpay)) as overdue_days
    from
    load
    where
    actually is null or actually > planpay
    group by
    1

    PostgreSQL, noqa
    alexnone
        2
    alexnone  
       2017-01-23 17:13:46 +08:00
    当天首次出现逾期超过 30 天的合同号和逾期天数

    这句话有点不好理解 按我理解就是 31 天欸
    staticor
        3
    staticor  
       2017-02-14 10:07:17 +08:00
    1 将 id 先根据还款期次和首次应还款日期, 展开之后的 N 期还款日期;

    2 等额(本息)还款, 把实际还款日期和上面的应还日期取 diff, 得到逾期日期;

    3 考虑提前还款的问题;

    辅助函数 rank() datediff()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5675 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:32 · PVG 10:32 · LAX 19:32 · JFK 22:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.