日期:2014-05-18 浏览次数:20556 次
select 
        storeId,
        itemId,
        sum([count]) as [count],
        sum(case when status='入库' then [count] else 0 end) as CountIn,
        sum(case when status<>'入库' then [count] else 0 end) as CountOut,
        sum(price) as price,
        sum(case when status<>'售出' then [price] else 0 end) as priceIn,
        sum(case when status='售出' then [price] else 0 end) as priceOut    
        
from A
group by storeId,itemId
order by storeId,itemId
------解决方案--------------------
select storeId,itemId,sum([count]) [count],
 sum(case status when '入库' then [count] else 0 end) countIn,
 sum(case status when '售出' then [count] else 0 end) countout,
 sum(price) price,
 sum(case status when '入库' then price else 0 end) priceIn,
 sum(case status when '售出' then price else 0 end) priceout
from A
group by storeId,itemId
order by storeId,itemId