Суммарные суммы SQL - PullRequest
       10

Суммарные суммы SQL

0 голосов
/ 05 августа 2020

У меня есть таблица расходов для каждой даты, как показано ниже

enter image description here

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). For 09/10/19 it should be the same as above as it's still October. For 08/01/20, 4+5+6=15. Can someone please help on how to achieve this. Thank you.

введите описание изображения здесь

1 Ответ

0 голосов
/ 05 августа 2020

Один метод - коррелированный подзапрос:

select p.*,
       (select sum(c.netamount)
        from charges c
        where c.date <= p.paymentdate
       ) as expected
from payments p;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...