sql - Cumulative sum across two tables -
i have 2 tables bill & payment. have show balance sheet these 2 tables.
the data in tables are:
tblbill
tblpayment
my current output is:
the query i'm trying use is:
select particulars,date,billamount,0'paidamount' tblbill union select particulars,date,0'billamount',paidamount tblpayment order date
however, need output in format:
is possible required format?
there go: assuming there 1 transaction in day....
with tb1 (select date,particulars,billamount,0'paidamount' tblbill union select date,particulars,0'billamount',paidamount tblpayment ) select t1.particulars,t1.[date],t1.[billamount],t1.[paidamount],(sum(t2.billamount) - sum(t2.paidamount)) balance tb1 t1 inner join tb1 t2 on t1.[date] >= t2.[date] group t1.particulars,t1.[date],t1.[billamount],t1.[paidamount] order [date]
in case of more 1 transactions in day....
with tb0 ( select [date],particulars,billamount,0'paidamount' tblbill union select [date],particulars,0'billamount',paidamount tblpayment ) , tb1 ( select date,particulars,billamount,paidamount,row_number() on (order [date] asc) [orderid] tb0 ) select t1.particulars,t1.[date],t1.[billamount],t1.[paidamount],(sum(t2.billamount) - sum(t2.paidamount)) balance tb1 t1 inner join tb1 t2 on t1.[orderid] >= t2.[orderid] group t1.particulars,t1.[date],t1.[billamount],t1.[paidamount] order [date]
Comments
Post a Comment