I am struggling with Group By and Null, as below.
I have two tables like below
Table : 1 accountmast
companyID accname category
102 PURCHASE ACCOUNT Purchase Account
102 LOCAL PURCHASE Purchase Account
102 SALES ACCOUNT Sales Account
Table: 2 ledger
companyID name debit credit
102 PURCHASE ACCOUNT 4742.3
102 LOCAL PURCHASE 51106
102 SALES ACCOUNT 8010
102 SALES ACCOUNT 4330000
102 PURCHASE ACCOUNT 5480000
And I have queried as below:
select
case
when a.catagory ='Purchase Account' then
l.name
end as PurchaseAccount,
case
when a.catagory ='Purchase Account' then
sum(coalesce(l.debit,0))-sum(coalesce(l.credit,0) )
end as PurAmt,
case
when a.catagory = 'Sales Account' then
l.name
end as SalesAccount,
case
when a.catagory = 'Sales Account' then
sum(coalesce(l.credit,0))-sum(coalesce(l.debit,0) )
end as SalesAmt
from ledger l join accountmast a
on l.companyID=a.companyID
and l.name = a.accname
where l.companyID=102
and a.catagory IN('Purchase Account','Sales Account')
group by l.name,a.catagory
And the result is:
Purchase Account PurAmt Sales Account SalesAmt
LOCAL PURCHASE 51106.00 NULL NULL
PURCHASE ACCOUNT 5484742.30 NULL NULL
NULL NULL SALES ACCOUNT 4338010.00
And the requirement is:
Purchase Account PurAmt Sales Account SalesAmt
LOCAL PURCHASE 51106.00 SALES ACCOUNT 4338010.00
PURCHASE ACCOUNT 5484742.30
What is the solution?.
If I use Group By then it allows Null value to associate tables’ columns.
If I use MAX, MIN then it shows the single records. What do I have to do?
If anyone has better solution please suggest.
1 Answer