Group By
以下のように、とに苦労してNull
います。
私は以下のような2つのテーブルを持っています
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
、単一のレコードが表示されます。私は何をしなければなりませんか?
誰かがより良い解決策を持っているなら、提案してください。