1

Here I have 2 tables 1st table consist of 10 rows of acnumber column and 10 rows of opening_balance column. 2nd table consists of 3 of those acnumber in a column having made transaction several times mentioned in transaction_type column as 'Deposit' or 'Withdrawal' and transaction_amount column .

so I have to select all acnumber and their deposits made i.e opening balance+deposits.If no transaction available in table 2 then add opening balance and 0. I tried something like this

select a1.acnumber,a1.opening_balance+if (a1.acnumber not in (a2.acnumber),0,sum(a2.transaction_amount))
from account a1 left join trandetails a2
on a1.acnumber=a2.acnumber
where a2.transaction_type like'Deposit'
group by a2.acnumber;

But it returns only acnumber mentioned in both tables.Any suggestions???

1 Answer 1

1

You need several changes to the query. The where is undoing the left outer join for instance. You should move that condition to the on clause. Also, the group by columns should be the columns in the select that are not in aggregation functions.

So, try this

select a1.acnumber, a1.opening_balance + coalesce(sum(a2.transaction_amount), 0)
from account a1 left join
     trandetails a2
     on a1.acnumber = a2.acnumber and
        a2.transaction_type like 'Deposit'
group by a1.acnumber, a1.opening_balance;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.