select
COA.AccountNo
,AccountName
,(coalesce(DVDebit, 0) + coalesce(JVDebit, 0) + coalesce(CTDebit, 0)) as Debit
,(coalesce(CVCredit, 0) + coalesce(JVCredit, 0)) as Credit
from ChartOfAccounts as COA
left join
(
select
AccountNo
,sum(Credit) as CVCredit
from CreditVouchersBody
group by AccountNo
) as CreditVoucher
on COA.AccountNo = CreditVoucher.AccountNo
left join
(
select
AccountNo
,sum(Debit) as DVDebit
from DebitVouchersBody
group by AccountNo
) as DebitVoucher
on COA.AccountNo = DebitVoucher.AccountNo
left join
(
select
AccountNo
,sum(Debit) as JVDebit
,sum(Credit) as JVCredit
from JournalVouchersBody
group by AccountNo
) as JournalVoucher
on COA.AccountNo = JournalVoucher.AccountNo
left join
(
select
AccountNoPayTo
,sum(Amount) as CTDebit
from BankCheques
group by AccountNoPayTo
) as BankdCheque
on COA.AccountNo = BankdCheque.AccountNoPayTo
where
COA.IsDetailed = 'True'
and COA.AccountType = 'Expense';
This query was working fine and was taking values as per requirements. But now i have changed nothing in query but database values i changed, and this query is returning only 0. there are values in some tables in database. Can any one help me where i am wrong in this.
Does "returning only 0" means that it returns zero for the sums or do you mean that it doesn't return any rows?
If you mean that the sums are zero, then most likely the join doesn't work anymore. Have you changed the AccountNo in ChartOfAccounts or in the other tables?
If it doesn't return any rows, you have to check ChartOfAccounts, because the left joins won't reduce the number of rows you get.
If there are values in ChartOfAccounts, check the values of IsDetailed and AccountType and compare them against your where condition.
select distinct IsDetailed from ChartOfAccounts
select distinct AccountType from ChartOfAccounts
For the others: This is his query in a readable form: