Running total for payments to the issued invoices

限于喜欢 提交于 2021-01-29 10:12:57

问题


I have the following tables:

create table Invoices (InvoiceID int, InvoiceDate date, Total money);
insert into Invoices (InvoiceID, InvoiceDate, Total) values
(1,'2020-11-01', 20),
(2,'2020-11-01', 14),
(3,'2020-11-02', 40),
(4,'2020-11-02', 35),
(5,'2020-11-03', 10),
(6,'2020-11-04', 63),
(7,'2020-11-04', 42);
   
create table Payments (InvoiceID int, PaymentDate date, Total money);
insert into Payments (InvoiceID, PaymentDate, Total) values
(5,'2020-11-07', 10),
(6,'2020-11-08', 63),
(4,'2020-11-09', 35),
(2,'2020-11-10', 14),
(7,'2020-11-11', 42),
(11,'2020-11-13', 20),
(13,'2020-11-14', 15);

To obtain a running total using SQL Server 2005, I'm using the following script:

with DateRange as
(
  select convert(date, '2020-11-01') as DateValue
  union all
  select dateadd(day, 1, dr.DateValue)
  from DateRange dr
  where dr.DateValue < '2020-11-30'
),
InvoicedTotal as
(
  select dr.DateValue,
         isnull(sum(i.Total), 0) as Invoiced
  from DateRange dr
  left join Invoices i
    on i.InvoiceDate = dr.DateValue
  group by dr.DateValue
),
PaidTotal as
(
  select dr.DateValue,
         isnull(sum(p.Total), 0) as Paid
  from DateRange dr
  left join Payments p
    on p.PaymentDate = dr.DateValue
  group by dr.DateValue
)
select convert(varchar(10), dr.DateValue, 102) as [YYYY.MM.DD],
       it1.Invoiced as [Invoiced],
       it3.Invoiced as [CumInvoiced],
       pt1.Paid as [Paid],
       pt3.Paid as [CumPaid],
       it3.Invoiced - pt3.Paid as [RunningTotal]
from DateRange dr
join InvoicedTotal it1
  on it1.DateValue = dr.DateValue
join PaidTotal pt1
  on pt1.DateValue = dr.DateValue
cross apply ( select sum(it2.Invoiced) as Invoiced
              from InvoicedTotal it2
              where it2.DateValue <= dr.DateValue ) it3
cross apply ( select sum(pt2.Paid) as Paid
              from PaidTotal pt2
              where pt2.DateValue <= dr.DateValue ) pt3
order by dr.DateValue;

How to show in the report only the payments for the issued invoices in the timeframe specified?


回答1:


with DateRange as
(
  select convert(date, '2020-11-01') as DateValue
  union all
  select dateadd(day, 1, dr.DateValue)
  from DateRange dr
  where dr.DateValue < '2020-11-30'
),
InvoicedTotal as
(
  select dr.DateValue,
         isnull(sum(i.Total), 0) as Invoiced
  from DateRange dr
  left join Invoices i
    on i.InvoiceDate = dr.DateValue
  group by dr.DateValue
),
PaidTotal as
(
  select dr.DateValue,
         isnull(sum(case
                      when i.InvoiceDate between '2020-11-01' and '2020-11-30' -- check if matching invoice was found
                      then p.Total                 -- YES = include Total amount in sum
                      else 0                       -- NO  = exclude total amount from sum
                    end), 0) as Paid
  from DateRange dr
  left join Payments p
    on p.PaymentDate = dr.DateValue
  left join Invoices i
    on  i.InvoiceId = p.InvoiceId -- check for invoice related to payment
  group by dr.DateValue
)
select convert(varchar(10), dr.DateValue, 102) as [YYYY.MM.DD],
       it1.Invoiced as [Invoiced],
       it3.Invoiced as [CumInvoiced],
       pt1.Paid as [Paid],
       pt3.Paid as [CumPaid],
       it3.Invoiced - pt3.Paid as [RunningTotal]
from DateRange dr
join InvoicedTotal it1
  on it1.DateValue = dr.DateValue
join PaidTotal pt1
  on pt1.DateValue = dr.DateValue
cross apply ( select sum(it2.Invoiced) as Invoiced
              from InvoicedTotal it2
              where it2.DateValue <= dr.DateValue ) it3
cross apply ( select sum(pt2.Paid) as Paid
              from PaidTotal pt2
              where pt2.DateValue <= dr.DateValue ) pt3
order by dr.DateValue;

Fiddle by milo2011



来源:https://stackoverflow.com/questions/65216405/running-total-for-payments-to-the-issued-invoices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!