0

我只是在这个 Sql Transact 后面浪费了两到三个小时,并且对 Null Value Skip 感到困惑。

我有两个如下表:

Table 1: AccountMast
companyID   accname         category
102     PURCHASE  ACCOUNT   Purchase Account
102     SALES ACCOUNT       Sales Account

Table2: Legder

companyID   name            
102     PURCHASE ACCOUNT        
102     SALES ACCOUNT       

我加入它如下:

select
case
when a.catagory='Purchase Account' then
l.name 
end as PurchaseAccount,
case
when a.catagory = 'Sales Account' then
l.name   
end as SalesAccount
from ledger l join accountmast a
on l.companyID=a.companyID
and l.name = a.accname
where l.companyID=102
and a.catagory='Purchase Account' or a.catagory='Sales Account'
group by l.name,a.catagory

结果是:

PurchaseAccount     SaleAccount
PURCHASE ACCOUNT    NULL
NULL                SALES ACCOUNT

但我想要这样的结果:

PurchaseAccount     SaleAccount
PURCHASE ACCOUNT    SALES ACCOUNT

怎么做?

4

1 回答 1

1

删除您的GROUP BY条款:

select
    max(case when a.catagory = 'Purchase Account' then l.name end) as PurchaseAccount,
    max(case when a.catagory = 'Sales Account' then l.name end) as SalesAccount
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')
于 2012-06-05T07:44:47.643 回答