I am trying to create a budgetting system for my company. The requirements from accounting states that I should get the previous balance then add it to the received amount. Subtract the sum of prev balance and received amount from the withdrawals to get the Balance end. Below is my stored procedure:
- ALTER PROCEDURE [dbo].[fin_cash_flow_summary_LAK] --'2020-07-06', '2020-07-06'
- -- Add the parameters for the stored procedure here
- @DateStart nvarchar(50),
- @DateEnd nvarchar(50)
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- -- Insert statements for procedure here
- SELECT a.BankCode,
- a.BankAcctType,
- a.BankInformation,
- a.CurrencyType,
- (
- SELECT SUM(ISNULL(b.CreditKip,0) - ISNULL(b.DebitKip,0))
- FROM fin_Cash_Flow_Master b
- WHERE a.BankInformation = b.BankInformation AND a.CurrencyType='LAK' AND a.BankAcctType <> 'Fixed' AND b.TransactionDate < @DateStart
- ) AS PreviousBalance,
- SUM(CreditKip) AS [Receive],
- SUM(DebitKip) AS Withdraw,
- (SELECT SUM(ISNULL(CreditKip, 0) - ISNULL(DebitKip, 0)) FROM dbo.fin_Cash_Flow_Master AS b WHERE (a.BankInformation = BankInformation)
- AND (a.CurrencyType = 'LAK') AND (a.BankAcctType <> 'Fixed') AND (TransactionDate < @DateStart)) + SUM(CreditKip) - SUM(DebitKip) AS BalanceEnd
- FROM dbo.fin_Cash_Flow_Master a
- WHERE a.TransactionDate BETWEEN @DateStart AND @DateEnd AND a.CurrencyType = 'LAK' AND a.BankAcctType <> 'Fixed'
- GROUP BY BankCode, BankAcctType, BankInformation, CurrencyType, BankAccountNo
- END

Laxmidhar SahooPosted Jul 6, 2020, 4:04 AM
Jes SiePosted Jul 6, 2020, 3:58 AM
Laxmidhar SahooPosted Jul 6, 2020, 3:23 AM