我想编写一个SQL查询,该查询每天都计算出未出色的发票的总数,但找不到有关如何执行此操作的解决方案。我的桌子包括发票金额,创建日和还款日(如果尚未收到还款,那是空的)。
为了计算未偿还的发票金额,我需要获取所有尚未偿还的金额的总和,即还款日是空的或焦点之后。这是一个示例:
还款 |
创建日 |
日 |
100 |
2-7-2022 |
4-7-2022 |
100 |
2-7-2022 |
3-7-2022 |
300 300 |
3-7-2022 |
|
200 |
4-7-7-2022 |
|
400 |
5-7-2022 |
6-7-2022 |
100 |
5-7-2022 |
|
100 |
5-7-2022 |
|
300 |
6-7-2022 |
|
现在我想计算每个发票的未发票金额 天。我想返回一张看起来如下的桌子。偿还的金额不包括在每日总数中。
日期 |
未偿发票 |
2-7-2022 |
200 |
3-7-2022 |
400 |
4-7-2022 |
500 |
5-7-2022 |
1100 |
6-7-2022 |
1000 |
希望对此获得一些投入。
谢谢!
I would like to write an SQL query that calculates the total of outstanding invoices on a daily basis, but couldn't find a solution as to how to do it so far. My table includes an invoice amount, a creation day and a repayment day (which is empty if no repayment has been received yet).
In order to calculate the outstanding invoice amount, I need to get the sum of all amounts that have not been repaid, i.e. the repayment day is empty or after the focal day. Here is an example:
amount |
creation day |
repayment day |
100 |
2-7-2022 |
4-7-2022 |
100 |
2-7-2022 |
3-7-2022 |
300 |
3-7-2022 |
|
200 |
4-7-2022 |
|
400 |
5-7-2022 |
6-7-2022 |
100 |
5-7-2022 |
|
100 |
5-7-2022 |
|
300 |
6-7-2022 |
|
Now I wish to calculate the outstanding invoice amount at the end of each day. I would like to have a table returned that looks like the following. The amounts that have been repaid are excluded from the daily total.
Date |
Outstanding invoice |
2-7-2022 |
200 |
3-7-2022 |
400 |
4-7-2022 |
500 |
5-7-2022 |
1100 |
6-7-2022 |
1000 |
Would love to get some input on this.
Thanks!
发布评论
评论(1)
第一步是将单个记录中可能发生的两件事分为两个单独的行(创建和可选还款)。您可以通过两次“循环”在桌子上“循环”,查看它以创建并查看它以偿还:
然后,您将其结果进行分组,每个日期的总和数量,然后使用窗口总和来获得一个运行总日期总和的总计:
“
The first step is to split the two things that can happen in a single record into two separate rows (creation and optional repayment). You do this by "looping" over the table twice, looking at it for creation and then looking at it for repayment:
Then you take the results of that, group by and sum amounts for each date, and use a window sum to get a running total of per date sums:
fiddle