Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There are 2 tables Ledger and transaction

Ledger Table has below fields

LID,LName,Lgroup,Opbal

Transaction Table has Txno,Tandate,LID,TXnType,Amount,Narration

Below Are The data

Ledger Table

LID, LName, Lgroup, Opbal DrCr
1 Salary Expense 10000 DR
2 Cash Asset 20000 DR
3 Electricty Expense 1000 DR


Transaction Table has

Txno, Tandate, LID, debit credit
1 2022-01-01 2 10000
1 2022-01-01 1 2000
1 2022-01-01 3 8000

Required Ledger Display

If Cash Ledger is selected

Txno Txndate Lanme Debit Credit Balance
1 2022-01-01 Salary 2000 18000
1 2022-01-01 Electricty 8000 10000
If Salary Ledger is selected

Txno Txndate Lanme Debit Credit Balance
1 2022-01-01 cash 2000 12000
If Electricity Ledger is selected

Txno Txndate Lanme Debit Credit balance
1 2022-01-01 cash 8000 7000
Have tried the below SQL but it does not work - for example if I query Salary ledger the all the ledger including Salary ledger is displayed, rather than the expected result I showed above.

What I have tried:

SELECT T1.TxnNo,
       T1.Txndate,
       t2.LName,
       ISNULL(T1.Debit, 0) AS Dr,
       ISNULL(T1.Credit, 0) AS Cr
FROM [Transaction] T1
     INNER JOIN (SELECT LID,
                        LName
                 FROM Ledger
                 WHERE LID IN (SELECT ledger.LID FROM Ledger WHERE LID = 3)) t2 ON T1.Lid = t2.LID
     INNER JOIN (SELECT TxnNo, Lid FROM [Transaction] WHERE Lid = 3) t3 ON t3.TxnNo = T1.TxnNo                                                                    ;
Posted
Updated 30-Jun-22 18:02pm
v2
Comments
CHill60 30-Jun-22 5:21am    
Part of the problem with trying to help you is that the column names in your query do not match the schema of the tables with the sample data - e.g. there are no columns called Debit and Credit - help us to help you and give us the correct table schemas/sample data.

1 solution

Your joins are overly complex.

Look at the sub-query
SQL
SELECT LID,
                        LName
                 FROM @Ledger
                 WHERE LID NOT IN (SELECT LID FROM @Ledger WHERE LID = 3)
That is exactly the same as
SQL
SELECT LID,
                        LName
                 FROM @Ledger
                 WHERE LID <> 3
Except the 2nd version is more performant and easier to understand.

Now I can see that you are attempting to extract records where LID is not 3 AND where LID is 3 - which actually returns nothing. Perhaps this article will help explain why Visual Representation of SQL Joins[^]

Your expected results do not seem to match the sample data you provided - for example the Cash Ledger is listing everything except Cash. Try something like the following
SQL
SELECT T1.TxnNo,
       T1.Txndate,
       L.LName,
       ISNULL(T1.Debit, 0) AS Dr,
       ISNULL(T1.Credit, 0) AS Cr
FROM @Transaction T1
INNER JOIN @Ledger L ON T1.Lid = L.LID
WHERE L.LID = 2 --Cash. Change to 1 for Salary or 3 for Electricity
-- Alternatively use WHERE L.LName = 'Cash' for clarity
You also appear to need a running balance. Try googling for "SQL Running Total" e.g. https://betterprogramming.pub/4-ways-to-calculate-a-running-total-with-sql-986d0019185c[^]
 
Share this answer
 
Comments
srinivas seshadri 30-Jun-22 7:19am    
If cash Ledger is selected the corresponding Ledgers posted in cash account as per the voucher should be displayed .That is Accounting Logic. Cash Should NOT be displayed. You query output is not as per required
CHill60 30-Jun-22 9:01am    
I'm not an accountant, I'm a developer who happens to have shown the OP where their problems lie with their code. Feel free to post a solution that gives the results the OP wanted for their homework - assuming they share their actual data with you in order to achieve that
Wave 13536632 30-Jun-22 9:42am    
That is y I have told you the accounting logic.You have apply system logic of displaying corresponding ledgers. Forget opening balance that i will take care
CHill60 30-Jun-22 9:53am    
You haven't told me anything in your question about accounting logic, you supplied sample data and expected results that did not match that sample data. I have fixed the problem with your code, you apply the necessary logic e.g. use WHERE L.LID <> 2.
However, your comment makes me believe that you are actually srinivas seshadri and are using two accounts on this site. That is suspicious behaviour so you have both been put on a watch list
Wave 13536632 1-Jul-22 0:05am    
As per the accounting Logic the corresponding ledgers posted in a/c which is queried to be displayed with the respective balance. i,e if you query salary A/c Cash A/c to be displayed with balance of 2000 against the same.Hope it is clear now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900