计算在任何时间点到期的金额?
我有两张表,一张显示已支付的所有付款,另一张是一张时间表,显示我们预计何时收到这些付款以及我们预计收到多少金额,如下所示。
我我正在尝试找出如何添加我的第一个表的一列,用于查找在第一个表中付款时我们预期有多少钱。因此,对于我的示例,我应该能够看到,当我们在 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..
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..
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
max(expected) ... where date <= DatePaid
添加时间表中的数字,然后使用sum() over
来获取运行总计。然后,我们使用这些数字来计算最后一列中提前支付的金额,或者如果为负数则计算后面支付的金额。
db<>fiddle 此处
We can add the figures from schedule using
max(expected) ... where date <= DatePaid
and then usesum() over
to get the running total.We then use these figures to calculate the amount paid in advance, or behind if negative, in the last column.
db<>fiddle here