简体   繁体   English

累计总数 SQL

[英]Cumulative totals SQL

I have a table for charges as below for each date我有一张每个日期的收费表如下

在此处输入图像描述

For each payment date as below, I need to calculate the expected amount and expected total amount based on the charges from the first table.对于以下每个付款日期,我需要根据第一个表中的费用计算预期金额和预期总金额。 For 02/10/2019, it is 1+2+3=6 (the charges after July until October). 2019 年 2 月 10 日为 1+2+3=6(7 月后至 10 月的费用)。 For 09/10/19 it should be the same as above as it's still October. 2019 年 9 月 10 日应该和上面一样,因为它仍然是 10 月。 For 08/01/20, 4+5+6=15.对于 08/01/20,4+5+6=15。 Can someone please help on how to achieve this.有人可以帮助如何实现这一目标。 Thank you.谢谢你。

在此处输入图像描述

One method is a correlated subquery:一种方法是相关子查询:

select p.*,
       (select sum(c.netamount)
        from charges c
        where c.date <= p.paymentdate
       ) as expected
from payments p;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM