2014-12-20 37 views
1
select 
    COUNT(Table1.ID) as count_shipped, 
    null as count_shipped 
from Table1 
where 
    table1.saleStatus='shipped' 
    and table1.saleApproved='yes' 

union 

select 
    null, 
    COUNT(Table1.ID) as count_pending 
from Table1 
where 
    table1.saleStatus in ('Pending', 'awaiting payment', 'backorder') 

合併行。這給出了這樣的輸出用NULL列

count_shipped  count_shipped 

NULL    5 
4    NULL 

,但我不想空我只希望在一個4行5任何人可以幫助我如何做到這一點的SQL Server?

回答

4

可以使用case總結一下你的條件

select sum(case when saleStatus = 'shipped' and table1.saleApproved = 'yes' 
       then 1 
       else 0 
      end) as count_shipped, 
     sum(case when saleStatus in ('Pending', 'awaiting payment', 'backorder') 
       then 1 
       else 0 
      end) as count_pending 
from Table1 
+0

太感謝你了,先生 – user3370649