我正在努力Group By
和Null
,如下所示。
我有两个如下表
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
我查询如下:
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
结果是:
Purchase Account PurAmt Sales Account SalesAmt
LOCAL PURCHASE 51106.00 NULL NULL
PURCHASE ACCOUNT 5484742.30 NULL NULL
NULL NULL SALES ACCOUNT 4338010.00
并且要求是:
Purchase Account PurAmt Sales Account SalesAmt
LOCAL PURCHASE 51106.00 SALES ACCOUNT 4338010.00
PURCHASE ACCOUNT 5484742.30
解决办法是什么?。
如果我使用Group By
它,它允许Null
值关联表的列。
如果我使用MAX
,MIN
那么它会显示单个记录。我需要做什么?
如果有人有更好的解决方案,请提出建议。