Suppose I have two table same as above
Accounts
|
ACCTID |
ACCTNAME |
ACCT TYPE |
Opening Balance |
Date |
|
12 |
CASH ACCT |
DEBIT |
1200 |
1-4-2023 |
|
13 |
BANK ACCT |
DEBIT |
1300 |
1-4-2023 |
|
14 |
SUPPLIER ACCT 1 |
CREDIT |
1400 |
1-4-2023 |
|
15 |
SUPPLIER ACCT 2 |
CREDIT |
800 |
1-4-2023 |
|
16 |
EXPESNES1 |
DEBIT |
900 |
1-4-2023 |
Transactions Table
|
Accno |
Acct Name |
Acct Type |
Debit amt |
Credit amt |
Date |
|
12 |
CASH ACCT |
DEBIT |
5000 |
0 |
1-4-2023 |
|
12 |
CASH ACCT |
DEBIT |
1000 |
0 |
1-4-023 |
|
12 |
CASH ACCT |
CREDIT |
0 |
2000 |
1-4-2023 |
|
12 |
CASH ACCT |
DEBIT |
1000 |
0 |
2-4-2023 |
|
13 |
BANK ACCT |
CREDIT |
0 |
200 |
1-4-2023 |
|
13 |
BBANK ACCT |
DEBIT |
4000 |
0 |
2-4-2023 |
|
13 |
BANK ACCT |
CREDIT |
300 |
0 |
3-4-2023 |
|
14 |
SUPPLIER ACCT |
CREDIT |
0 |
400 |
1-4-2023 |
|
14 |
SUPPLIER ACCT |
CREDIT |
0 |
300 |
3-4-2023 |
|
15 |
SUPPLIER ACCT 2 |
DEBIT |
2000 |
0 |
1-4-2023 |
|
15 |
SUPPLIER ACCT 2 |
CREDIT |
0 |
1000 |
4-4-2023 |
I HVE TO DISPLAY VALUE LIKE THIS
Opening balance will show according to credit and debit acct type for example
From date : 2023-04-01 to date 2023-04-02
|
Acct name |
Opening balance(cr) |
Opening balance (dr) |
Credit(sum) |
Debit(sum) |
Closing credit |
Closing debit |
|
Casah acct |
0 |
1200 |
2000 |
6000 |
0 |
5200 |
|
Bank acct |
0 |
1300 |
600 |
4000 |
0 |
4700 |
|
Supplier acct |
1400 |
0 |
700 |
100 |
2200 |
0 |
|
Supplier acct2 |
800 |
0 |
2000 |
2000 |
1800 |
|
|
|
|
|
|
|
|
|
Opening balance will show debit side and credit side according to account type
And opening balance will be change according to date . I mean closing balance will become to opening balance on next date
I have to display the record according to date range wise (date between wise )
Please help me in this situation I will highly thank full to you

Amit MohantyRank #112 · 20K reputationPosted Apr 5, 2023, 1:32 AM EDT
Check this
;WITH OpeningBalance AS ( SELECT tblAccounts.ACCTID, tblAccounts.ACCTNAME, SUM(CASE WHEN tblAccounts.ACCTTYPE = 'CREDIT' THEN tblAccounts.OpeningBalance ELSE 0 END) AS OpeningBalanceCredit, SUM(CASE WHEN tblAccounts.ACCTTYPE = 'DEBIT' THEN tblAccounts.OpeningBalance ELSE 0 END) AS OpeningBalanceDebit FROM tblAccounts WHERE tblAccounts.Date BETWEEN '2023-04-01' AND '2023-04-05' GROUP BY tblAccounts.ACCTID, tblAccounts.ACCTNAME ), TransactionTotals AS ( SELECT tblTransactions.AccNo, tblTransactions.AcctName, SUM(tblTransactions.CreditAmt) AS CreditTotal, SUM(tblTransactions.DebitAmt) AS DebitTotal FROM tblTransactions WHERE tblTransactions.Date BETWEEN '2023-04-01' AND '2023-04-05' GROUP BY tblTransactions.AccNo, tblTransactions.AcctName ) SELECT ob.ACCTNAME AS 'Account Name', ob.OpeningBalanceCredit AS 'Opening Balance (cr)', ob.OpeningBalanceDebit AS 'Opening Balance (dr)', COALESCE(tt.CreditTotal, 0) AS 'Credit(sum)', COALESCE(tt.DebitTotal, 0) AS 'Debit(sum)', CASE WHEN ob.OpeningBalanceCredit = 0 THEN 0 ELSE ABS((COALESCE(ob.OpeningBalanceCredit, 0) + COALESCE(tt.CreditTotal, 0)) - (COALESCE(ob.OpeningBalanceDebit, 0) + COALESCE(tt.DebitTotal, 0))) END AS 'Closing Credit', CASE WHEN ob.OpeningBalanceDebit = 0 THEN 0 ELSE ABS((COALESCE(ob.OpeningBalanceDebit, 0) + COALESCE(tt.DebitTotal, 0)) - (COALESCE(ob.OpeningBalanceCredit, 0) + COALESCE(tt.CreditTotal, 0))) END AS 'Closing Debit' FROM OpeningBalance ob LEFT JOIN TransactionTotals tt ON ob.ACCTID = tt.AccNo AND ob.ACCTNAME = tt.AcctName;Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 5, 2023, 1:10 AM EDT
Can you please try the below query and let me know, if have any issue.
SELECT a.ACCTNAME AS 'Account Name', COALESCE(SUM(CASE WHEN a.[ACCT TYPE] = 'CREDIT' THEN t.[Credit amt] ELSE 0 END) + a.[Opening Balance], 0) AS 'Opening Balance (Cr)', COALESCE(SUM(CASE WHEN a.[ACCT TYPE] = 'DEBIT' THEN t.[Debit amt] ELSE 0 END) + a.[Opening Balance], 0) AS 'Opening Balance (Dr)', COALESCE(SUM(t.[Credit amt]), 0) AS 'Credit (Sum)', COALESCE(SUM(t.[Debit amt]), 0) AS 'Debit (Sum)', COALESCE(SUM(CASE WHEN t.[Date] <= @toDate THEN t.[Credit amt] ELSE 0 END), 0) AS 'Closing Credit', COALESCE(SUM(CASE WHEN t.[Date] <= @toDate THEN t.[Debit amt] ELSE 0 END), 0) AS 'Closing Debit' FROM Accounts a LEFT JOIN Transactions t ON a.ACCTID = t.Accno AND a.[ACCT TYPE] = t.[Acct Type] AND t.[Date] BETWEEN @fromDate AND @toDate GROUP BY a.ACCTID, a.ACCTNAME, a.[ACCT TYPE], a.[Opening Balance] ORDER BY a.ACCTID;