SQL的逾期发展

发布于 2025-01-20 20:38:48 字数 1462 浏览 4 评论 0原文

尽管我做出了努力,但我无法想到一种方法来满足我的需求:

我有2个表格,分别包含一套贷款要偿还的贷款,以及对这些贷款的一系列偿还(并非所有贷款都有报价,因为其中一些贷款都可以参与其中保持无薪)。

贷款有列:

  • ID
  • TABLE
  • DUE_DATE

偿还具有列:

  • ID
  • 债务_id
  • paymend_date

我的目标是获得逾期5天以上日期以上的金额过去的。

这是我位于(在Postgres语法中)的位置:

with due_for_5_days_by_date AS (
    select 
    due_date + interval '5' DAY as due_date,
    sum(amount) as amount
    FROM loan
    GROUP BY due_date
),
paid_by_date as (
    select 
        payment_date,
        sum(amount) as amount
    FROM reimbursement
    join loan on loan.id = loan_id
    GROUP BY payment_date
),
cumulated as (
    SELECT
        COALESCE(due_date, payment_date) as date,
        SUM(COALESCE(paid_by_date.amount, 0)) over (order by COALESCE(due_date, payment_date)) as paid,
        SUM(COALESCE(due_for_5_days_by_date.amount, 0)) over (order by COALESCE(due_date, payment_date)) as due_5d
    FROM due_for_5_days_by_date
    FULL OUTER JOIN paid_by_date on due_date = payment_date
)
select 
    DATE,
    due_5d - paid as overdue_5d
from cumulated
order by date

说明:

  • 首先,我获得每个日期恰好5天的金额
  • ,然后我得到每个日期付款的金额
  • ,然后我将所获得的金额计入1。和2。获取累积的应付金额和付费金额
  • ,然后按差额我获得逾期金额

,但问题是,通过此任务,按日期支付的金额还包含贷款的报销,该贷款持续了少于5天。换句话说,最终查询中每个日期的付费金额被高估了,因为它应该排除该日期最近到期的贷款金额。我想不出一种解决这个问题的方法。

您能想到一种获得我想要的东西吗?

非常感谢您的帮助!

Despite my efforts I cannot think of a way to answer my need :

I have 2 tables containing respectively a set of loans to be reimbursed, and a set of reimbursements on these loans (not all loans have an entry in reimbursement table because some of them remain unpaid).

Table loan has columns:

  • id
  • amount
  • due_date

Table reimbursement has columns:

  • id
  • debt_id
  • payment_date

My goal is to obtain the amount that was overdue for more than 5 days for each date in the past.

Here is where I am at (in Postgres grammar):

with due_for_5_days_by_date AS (
    select 
    due_date + interval '5' DAY as due_date,
    sum(amount) as amount
    FROM loan
    GROUP BY due_date
),
paid_by_date as (
    select 
        payment_date,
        sum(amount) as amount
    FROM reimbursement
    join loan on loan.id = loan_id
    GROUP BY payment_date
),
cumulated as (
    SELECT
        COALESCE(due_date, payment_date) as date,
        SUM(COALESCE(paid_by_date.amount, 0)) over (order by COALESCE(due_date, payment_date)) as paid,
        SUM(COALESCE(due_for_5_days_by_date.amount, 0)) over (order by COALESCE(due_date, payment_date)) as due_5d
    FROM due_for_5_days_by_date
    FULL OUTER JOIN paid_by_date on due_date = payment_date
)
select 
    DATE,
    due_5d - paid as overdue_5d
from cumulated
order by date

Explanation :

  • First I get the amount due for exactly 5 days for each date
  • Then, I get the amount paid for each date
  • Then I sum the amounts obtained at 1. and 2. to get the cumulative due and paid amounts
  • Then by difference I get the overdue amount

But the problem is that, by doing this, the amount paid by date also contains reimbursement of loans expired for less than 5 days. In other words the paid amount for each date in the final query is overestimated as it should exclude the amount received on loans recently expired on that date. I cannot think of a way to fix this.

Can you think of a way to obtain what I want ?

Thanks a lot for your help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2025-01-27 20:38:48

我刚刚找到了这种方法,但是它使用了一个十字连接,因此不确定它在我的整个数据上是否会很好地扩展,即使它是在BigQuery上托管的:

with all_dates as (
    select distinct payment_date as date
    from reimbursement
    union
    select distinct due_date as date
    from loan
    union
    select distinct due_date + interval '5' day as date
    from loan
), 
joined_data as (
    SELECT
        amount,
        due_date,
        payment_date
    from loan
    LEFT join reimbursement on loan.id = loan_id
)
select 
    date,
    SUM(
      CASE
        WHEN (due_date <= DATE - INTERVAL '5' day) and (payment_date > DATE or payment_date is null) then amount
        else 0
      END
    ) as overdue_5d
FROM all_dates
cross join joined_data
group by date
order by DATE

I just found this way of doing it, but it uses a cross join, so not sure it will scale well on my whole data, even though it is hosted on BigQuery:

with all_dates as (
    select distinct payment_date as date
    from reimbursement
    union
    select distinct due_date as date
    from loan
    union
    select distinct due_date + interval '5' day as date
    from loan
), 
joined_data as (
    SELECT
        amount,
        due_date,
        payment_date
    from loan
    LEFT join reimbursement on loan.id = loan_id
)
select 
    date,
    SUM(
      CASE
        WHEN (due_date <= DATE - INTERVAL '5' day) and (payment_date > DATE or payment_date is null) then amount
        else 0
      END
    ) as overdue_5d
FROM all_dates
cross join joined_data
group by date
order by DATE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文