计算在任何时间点到期的金额?

发布于 2025-01-17 04:57:07 字数 626 浏览 0 评论 0原文

我有两张表,一张显示已支付的所有付款,另一张是一张时间表,显示我们预计何时收到这些付款以及我们预计收到多少金额,如下所示。

在此处输入图像描述

我我正在尝试找出如何添加我的第一个表的一列,用于查找在第一个表中付款时我们预期有多少钱。因此,对于我的示例,我应该能够看到,当我们在 2022 年 1 月 11 日收到 500 条时,我们预计当时会收到 490 条。当我们于 2022 年 2 月 4 日收到 50 份时,我们预计当时会收到 550 份。因此,我可以添加一列来计算如下所示的差异。

在此处输入图像描述

我已写出查询以加入发票ID,这是很简单的一点..我只是无法得到我的考虑如何加入日期以获得我需要的输出。

实现这一目标的最佳方法是什么,有人能指出我正确的方向吗?

I have 2 tables, one shows me any payments that were made and another is a schedule that shows me when we expected to receive these payments and how much we expect to receive, like the below..

enter image description here

I am trying to work out how to add a column to my first table that looks up how much we expected to have at the time a payment was made in the first table. So for my example, I should be able to see that when we received 500 on 11 Jan 2022, we expected to have 490 at that point in time. And when we received 50 on 4th feb 2022, we expected to have 550 at that point in time. So I would be able to add a column to calculate differences like the below..

enter image description here

I have my query written out to join on the invoiceID which is the easy bit.. I just can't get my head around how I would join on the dates to get the output I need.

What would be the best way to achieve this, can someone point me in the correct direction?

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

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

发布评论

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

评论(1

简单爱 2025-01-24 04:57:07

我们可以使用 max(expected) ... where date <= DatePaid 添加时间表中的数字,然后使用 sum() over 来获取运行总计。
然后,我们使用这些数字来计算最后一列中提前支付的金额,或者如果为负数则计算后面支付的金额。

创建付款表 (
发票ID int,
付款金额 int,
date支付日期);
插入付款值
(493,500,'2022-01-11'),
(493,50,'2022-02-04'),
(494,500,'2022-02-01'),
(494,100,'2022-03-01');
创建表计划(
调度 ID int,
发票ID int,
预期金额 int,
预期日期日期);
插入计划值
(1,493,490,'2022-01-10'),
(2,493,550,'2022-02-03'),
(3,494,300,'2022-02-28');

<前><代码>选择
p.支付日期,
p.invoiceId,
p.付款金额,
总和(付款金额)超过
(按 p.InvoiceId 分区
按 p.datePaid 排序)TotalPaid,
max(s.expectedAmount) 预期,
总和(付款金额)超过
(按 p.InvoiceId 分区
按 p.datePaid 订购)
- 最大(s.expectedAmount)提前
从付款 p
在 p.invoiceId = s.invoiceID 上加入计划 s
其中 p.datePaid >= s.expectedDate
分组依据
p.invoiceId,
p.付款金额,
p.支付日期
按 p.datePaid 订购

付款日期 |发票编号 |付款金额 |总付费 |预计 |进步
:--------- | --------: | ------------: | --------: | --------: | ------:
2022年1月11日 | 493 | 493 500 | 500 500 | 500 490 | 490 10
2022-02-04 | 493 | 493 50 | 50 550 | 550 550 | 550 0
2022-03-01 | 494 | 494 100 | 100 100 | 100 300 | 300 -200

db<>fiddle 此处

We can add the figures from schedule using max(expected) ... where date <= DatePaid and then use sum() overto get the running total.
We then use these figures to calculate the amount paid in advance, or behind if negative, in the last column.

create table payments (
invoiceId int,
paymentAmount int,
datePaid date);
insert into payments values
(493,500,'2022-01-11'),
(493,50,'2022-02-04'),
(494,500,'2022-02-01'),
(494,100,'2022-03-01');
create table schedule (
scheduleId int,
invoiceId int,
expectedAmount int,
expectedDate date);
insert into schedule values
(1,493,490,'2022-01-10'),
(2,493,550,'2022-02-03'),
(3,494,300,'2022-02-28');
select
  p.datePaid,
  p.invoiceId,
  p.paymentAmount,
  sum(paymentAmount) over 
      (partition by p.InvoiceId 
      order by p.datePaid) TotalPaid,
  max(s.expectedAmount) Expected,
  sum(paymentAmount) over 
      (partition by p.InvoiceId 
      order by p.datePaid) 
  - max(s.expectedAmount) Advance
from payments p
join schedule s on p.invoiceId = s.invoiceID
where p.datePaid >= s.expectedDate
group by 
  p.invoiceId,
  p.paymentAmount,
  p.datePaid 
order by p.datePaid
datePaid   | invoiceId | paymentAmount | TotalPaid | Expected | Advance
:--------- | --------: | ------------: | --------: | -------: | ------:
2022-01-11 |       493 |           500 |       500 |      490 |      10
2022-02-04 |       493 |            50 |       550 |      550 |       0
2022-03-01 |       494 |           100 |       100 |      300 |    -200

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文