I need expected output
Code
id RefNo TransDate Dr Cr Balance
----------- ---------- ---------- --------------------- ---------------------
Opning balance 40
3 003 2013-01-03 300.00 0.00 340
4 004 2013-01-04 0.00 100.00 240
5 004 2013-01-05 200.00 0.00 440Best Regards
Sanjeev

Satya KarkiRank #78 · 24.6K reputationPosted Jan 8, 2024, 9:46 AM EST
SELECT id, RefNo, TransDate, Dr, Cr, SUM(Dr - Cr) OVER (ORDER BY TransDate, id) AS Balance FROM TableName;
Aman GuptaRank #193 · 12.4K reputationPosted Jan 8, 2023, 11:43 AM EST
Hi Sanjeev,
Please find the query here;
https://dba.stackexchange.com/questions/264098/how-to-retrieve-credit-debit-and-balance-from-more-than-one-table-in-sql-server
Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 8, 2023, 6:11 AM EST
Hi , Please follow this
https://www.codeproject.com/Questions/1230949/Credit-debit-balance-calculation-on-SQL
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 7, 2024, 1:02 PM EST
To achieve this, you can use a combination of CASE statements and the SUM() window function over an ordered set of records.
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 7, 2024, 1:04 PM EST
WITH TransactionData AS ( SELECT id, RefNo, TransDate, Dr, Cr, ROW_NUMBER() OVER (ORDER BY TransDate, id) AS RowNum FROM YourTableName ) SELECT id, RefNo, TransDate, Dr, Cr, CASE WHEN RowNum = 1 THEN 'Opning balance' ELSE SUM(Dr - Cr) OVER (ORDER BY TransDate, id) -- Running balance END AS Balance FROM TransactionData;The query uses a Common Table Expression (CTE) named TransactionData to assign a row number (RowNum) based on the TransDate and id. Then, in the main query, it calculates the Balance using the SUM() window function over the ordered transactions.
Amit MohantyRank #112 · 20K reputationPosted Jan 8, 2024, 1:06 AM EST
Can you please provide your table structure with some sample data?
To get better responce please provide enough code like table structure with sample data and what you have done so far, so that others can better understand the problem.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 8, 2024, 1:13 AM EST
To create a query that calculates the debit, credit, and balance without using a subquery, you can use a combination of window functions and conditional statements in SQL.
Here's an example query that achieves the desired output:
SELECT id, RefNo, TransDate, CASE WHEN Dr > 0 THEN Dr ELSE 0 END AS Dr, CASE WHEN Cr > 0 THEN Cr ELSE 0 END AS Cr, SUM(CASE WHEN Dr > 0 THEN Dr ELSE -Cr END) OVER (ORDER BY TransDate, id) AS Balance FROM your_table ORDER BY TransDate, id;It calculates the debit, credit, and balance for each transaction based on the transaction date and ID.
I hope this helps! Let me know if you have any further questions.
Rijwan AnsariRank #82 · 24.2K reputationPosted Jan 8, 2024, 9:50 AM EST
Try this
SELECT id, RefNo, TransDate, Dr, Cr, SUM(Dr - Cr) OVER (ORDER BY id) + 40 AS Balance FROM TableName;
Mohammad HussainRank #513 · 4.8K reputationPosted Jan 11, 2024, 10:57 AM EST
SELECT account_id, SUM(CASE WHEN trans_type = 'credit' THEN amount ELSE 0 END) AS credits, SUM(CASE WHEN trans_type = 'debit' THEN amount ELSE 0 END) AS debits, SUM(CASE WHEN trans_type = 'credit' THEN amount WHEN trans_type = 'debit' THEN -amount ELSE 0 END) AS balance FROM transactions GROUP BY account_id;Anandu G NathRank #681 · 3.5K reputationPosted Jan 8, 2024, 5:31 AM EST
sanjeev roy
You can use running total mehode
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 12, 2024, 12:16 AM EST
Hello Sanjeev Roy,
Please refer,
SELECT Id, RefNo, TransactionDate, AccountID, SUM(CASE WHEN DebitOrCredit = 'Debit' THEN Amount ELSE 0 END) AS Dr , SUM(CASE WHEN DebitOrCredit = 'Credit' THEN Amount ELSE 0 END) AS Cr, SUM(CASE WHEN DebitOrCredit = 'Debit' THEN Amount ELSE -Amount END) OVER (PARTITION BY AccountID ORDER BY TransactionDate, RefNo) AS Balance FROM Transactions GROUP BY Id,RefNo, TransactionDate, AccountID;