为什么前一个sql可把卖转为0而后一个还是NULL呢?select isnull(卖,0)as 卖 from
(
select 股票名称,
(case 交易性质 when '买' then sum(交易数量) end)as 买, 
(case 交易性质 when '卖' then sum(交易数量) end)as 卖
from gpmm group by 股票名称,交易性质
)b
====================================================
select 股票名称,
(case 交易性质 when '买' then isnull(sum(交易数量),0) end)as 买, 
(case 交易性质 when '卖' then isnull(sum(交易数量),0) end)as 卖
from gpmm 
group by 股票名称,交易性质

解决方案 »

  1.   

    select 股票名称,
    isnull((case 交易性质 when '买' then sum(交易数量) end),0) as 买,
    isnull((case 交易性质 when '卖' then sum(交易数量) end),0) as 卖
    from gpmm
    group by 股票名称,交易性质
      

  2.   


    当 交易性质=买
    case 交易性质 when '卖' then isnull(sum(交易数量),0) end
    直接过去了,根本没有执行isnull(sum(交易数量),0)select 股票名称,
    (case 交易性质 when '买' then isnull(sum(交易数量),0) else 0 end)as 买, 
    (case 交易性质 when '卖' then isnull(sum(交易数量),0) else 0 end)as 卖
    from gpmm 
    group by 股票名称,交易性质