I need expected output
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 440
Best Regards
Sanjeev
Jignesh KumarPosted Jan 12, 2024, 5:16 AM
Hello Sanjeev Roy,
Please refer,
Mohammad HussainPosted Jan 11, 2024, 3:57 PM
Rijwan AnsariPosted Jan 8, 2024, 2:50 PM
Try this
SELECT id, RefNo, TransDate, Dr, Cr, SUM(Dr - Cr) OVER (ORDER BY id) + 40 AS Balance FROM TableName;
Satya KarkiPosted Jan 8, 2024, 2:46 PM
SELECT id, RefNo, TransDate, Dr, Cr, SUM(Dr - Cr) OVER (ORDER BY TransDate, id) AS Balance FROM TableName;
Anandu G NathPosted Jan 8, 2024, 10:31 AM
sanjeev roy
You can use running total mehode
Jayraj ChhayaPosted Jan 8, 2024, 6:13 AM
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:
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.
Amit MohantyPosted Jan 8, 2024, 6:06 AM
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.
Tuhin PaulPosted Jan 7, 2024, 6:04 PM
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.
Tuhin PaulPosted Jan 7, 2024, 6:02 PM
To achieve this, you can use a combination of CASE statements and the SUM() window function over an ordered set of records.
Aman GuptaPosted Jan 8, 2023, 4:43 PM
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 GovindarajPosted Jan 8, 2023, 11:11 AM
Hi , Please follow this
https://www.codeproject.com/Questions/1230949/Credit-debit-balance-calculation-on-SQL