SQL Server - SQL Getting Debit, Credit and Balance Issue - Database Administrators Stack Exchange
SQL Server - SQL Getting Debit, Credit and Balance Issue - Database Administrators Stack Exchange
sqlserverSqlgettingdebit,creditandbalanceissueDatabaseAdministratorsStackExchange
DatabaseAdministratorsStack
Exchangeisaquestionandanswersite
fordatabaseprofessionalswhowishto
improvetheirdatabaseskillsandlearn
fromothersinthecommunity.Join
themitonlytakesaminute:
signup
login
tour
Here'showitworks:
Anybodycanask
aquestion
help
Anybodycan
answer
Thebestanswersarevoted
upandrisetothetop
Signup
Sqlgettingdebit,creditandbalanceissue
IamusingthequerybelowtogetDebit/CreditBalancefromtable
statement
SELECTt.[InvoiceID],t.S_TypeasType,
t.Date,t.Debit,t.Credit,b.Balance
FROMStatementastCROSSapply
(SELECTBalance=SUM(Debit)SUM(Credit)
FROMStatementasx
WHERE(x.date<t.dateor
x.date=t.date
)AND
x.CustID=t.CustID
)b
WHEREt.CustID='48'anddatebetween'20150101'and'20160101'
ORDERBYt.date
output
InvoiceIDTypeDateDebitCreditBalance
51ServiceInvoice20150829500.000.00500.00
51ReceiptVoucher201509070.00500.000.00
76ServiceInvoice201509281000.000.001500.00
208SalesInvoice20150928500.000.001500.00
119SalesInvoice20151031500.000.002000.00
76ReceiptVoucher201511210.00500.00500.00
208ReceiptVoucher201511210.00500.00500.00
119ReceiptVoucher201511210.00500.00500.00
165ServiceInvoice20151201500.000.001000.00
165ReceiptVoucher201512220.00500.00500.00
224ServiceInvoice20151231500.000.001000.00
First,howcanIgeteachReceiptVoucherunderitsinvoice?
Second,whenIhavetheReceiptVoucherwithsamedateastheinvoicehowcanIbesureitshowsundertheInvoiceusing
getcorrectvaluesforthebalance?
orderbytype
to
TheexpectedoutputinvoicesorderedbydateanditsReceiptVoucherunderit.
InvoiceIDTypeDateDebitCreditBalance
51ServiceInvoice20150829500.000.00500.00
51ReceiptVoucher201509070.00500.000.00
76ServiceInvoice201509281000.000.001000.00
76ReceiptVoucher201511210.00500.00500.00
208SalesInvoice20150928500.000.001000.00
208ReceiptVoucher201511210.00500.00500.00
119SalesInvoice20151031500.000.001000.00
119ReceiptVoucher201511210.00500.00500.00
165ServiceInvoice20151201500.000.001000.00
165ReceiptVoucher201512220.00500.00500.00
224ServiceInvoice20151231500.000.001000.00
sqlserver
askedJan12at3:10
PaulWhite
29.8k
12
user3403112
171
271
123
2Answers
http://dba.stackexchange.com/questions/125955/sqlgettingdebitcreditandbalanceissue
1/3
12/5/2016
sqlserverSqlgettingdebit,creditandbalanceissueDatabaseAdministratorsStackExchange
Themainthingyouneedistogeneratetherequiredordersequenceusingrow_number().
seq=row_number()over
(
partitionbyt.CustID
orderbyt.InvoiceID,
t.Date,
CASEWHENt.S_Type='ReceiptVoucher'THEN1ELSE2END
)
thanuseitforcalculationofthecumulativebalance
;
WITHcte
AS(
SELECTCustID,
[InvoiceID],
S_Type,
DATE,
Debit,
Credit,
seq=row_number()OVER(
PARTITIONBYCustID
ORDERBYInvoiceID,
DATE,
CASE
WHENS_Type='ReceiptVoucher'
THEN1
ELSE2
END
)
FROMStatement
)
SELECTc.[InvoiceID],
c.S_TypeASType,
c.DATE,
.Debit,
c.Credit,
b.Balance
FROMctec
CROSSAPPLY(
SELECTBalance=SUM(Debit)SUM(Credit)
FROMcteASx
WHEREx.CustID=c.CustID
ANDx.seq<=c.seq
)b
WHEREc.CustID='48'
ANDDATEBETWEEN'20150101'
AND'20160101'
ORDERBYseq
editedJan13at0:44
answeredJan12at8:53
Squirrel
176
1 Thisanswerdoesnotseemtoprovidethewantedoutput.ypercubeJan13at9:10
Iamnotnearacomputerwithanydatabaseonittotrythisideahoweverlookingatyour
"expectedoutput",theinvoiceorsalesnumberisanincrementingnumber(assuminginvoice208
isreallyatypoandshouldbeinvoice108).Haveyoutried'OrderbyInvoiceIDASC,Debit
Desc'?Itwillkeeppaymentsforeachinvoiceundertheinvoice.Anotherpossibilityissomething
like'OrderbyinvoiceID,ifdebit>0then0else1endif,date'.
answeredJan13at21:39
GDD
54
Basedonthedesiredoutputposted,theOPdoesn'tjustwanttochangethesortingfortheoutputtheywantthe
cumulativebalancetofollowthesamesortingaswell.AndriyMJan14at8:27
Idon'tagreewith@AndriyMtherunningbalanceisbasedon"owed"(invoices)minus"payments"(receipts)todate
asofeachtransaction.I'mkindaconfusedbysortingbycumulativebalance?GDDJan14at12:59
http://dba.stackexchange.com/questions/125955/sqlgettingdebitcreditandbalanceissue
2/3
12/5/2016
sqlserverSqlgettingdebit,creditandbalanceissueDatabaseAdministratorsStackExchange
@GDD:Well,yes,absolutely,runningtotals/balancesaretypicallycalculatedfollowingtheorderofeventsasthey
werehappening.Thedesiredoutputinthiscase,however,clearlyindicatesadifferentorder.It'sprobablyeasierto
seestartingwithInvoiceID76:+1000(Balance=1000),500(Balance=1000500=500),+500(thisisanewinvoice
nowbutBalance=1000,meaningitincludesthepreviousbalanceof500).I'mwithyouinthatit'sveryunusualand
I'mreadytoassumethattheOPmaybeconfused,butstill,that'sthesortingspecifiedsofar.AndriyMJan14at
13:13
http://dba.stackexchange.com/questions/125955/sqlgettingdebitcreditandbalanceissue
3/3