计算SQL中有条件运行的总数

发布于 2025-02-13 17:51:16 字数 1144 浏览 0 评论 0 原文

我想编写一个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!

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

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

发布评论

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

评论(1

○闲身 2025-02-20 17:51:16

第一步是将单个记录中可能发生的两件事分为两个单独的行(创建和可选还款)。您可以通过两次“循环”在桌子上“循环”,查看它以创建并查看它以偿还:

select
    case payment
        when 1 then repayment_date
        else creation_date
    end date,
    case payment
        when 1 then -amount
        else amount
    end amount
from (select 0 payment union all select 1 payment) payment
join invoice on payment=0 or repayment_date

然后,您将其结果进行分组,每个日期的总和数量,然后使用窗口总和来获得一个运行总日期总和的总计:

select
    date_format(date,'%e-%c-%Y') Date,
    sum(sum(amount)) over (order by date) 'Outstanding invoice'
from (
    select
        case
            when payment then repayment_date
            else creation_date
        end date,
        case
            when payment then -amount
            else amount
        end amount
    from (select 0 payment union all select 1 payment) payment
    join invoice on payment=0 or repayment_date
) activity
group by date

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:

select
    case payment
        when 1 then repayment_date
        else creation_date
    end date,
    case payment
        when 1 then -amount
        else amount
    end amount
from (select 0 payment union all select 1 payment) payment
join invoice on payment=0 or repayment_date

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:

select
    date_format(date,'%e-%c-%Y') Date,
    sum(sum(amount)) over (order by date) 'Outstanding invoice'
from (
    select
        case
            when payment then repayment_date
            else creation_date
        end date,
        case
            when payment then -amount
            else amount
        end amount
    from (select 0 payment union all select 1 payment) payment
    join invoice on payment=0 or repayment_date
) activity
group by date

fiddle

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