Добрый день, ребята. В моем запросе я хочу добавить текущий баланс (из предыдущей подлежащей оплате) к следующей сумме поля и вычесть его к произведенному платежу (поле для оплаты)
примеры кодов
select a.vendorname
,DATENAME(month ,(cdvdate)) as Month
,year(cdvdate) as Year
, sum(b.credit) as Amount
, sum(b.debit) as Paid
, sum(b.credit)- sum(b.debit) as balance
from (select cdvno,acct
, sum(case when credit = 0 then debit else 0 end) as debit
, sum(case when debit = 0 then credit else 0 end)as credit
, trantype
from cdvdtl
group by cdvno,acct, trantype
)b
left join cdvhdr a
on b.cdvno = a.cdvno and b.trantype = a.trantype
left join account c on b.acct = c.acct
where b.acct='2122102'
group by a.vendorname,year(cdvdate),DATENAME(month ,(cdvdate))
order by Vendorname,case year(cdvdate)
when '2016' then 1
when '2017' then 2
when '2018' then 3
when '2019' then 4
end
,case DATENAME(month ,(cdvdate))
when 'January' then 1
when 'February' then 2
when 'March' then 3
when 'April' then 4
when 'May' then 5
when 'June' then 6
when 'July' then 7
when 'August' then 8
when 'September' then 9
when 'October' then 10
when 'November' then 11
when 'December' then 12
end
Я пытался использовать номер строки, но это не работает так, как я хочу
Я хочу, чтобы результат был примерно таким. Большое спасибо, просветите меня, пожалуйста