将行与NULL列组合在一起

时间:2014-12-20 11:37:06

标签: sql sql-server

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')

这给出了这个输出

count_shipped      count_shipped

NULL               5
 4                NULL

但是我不想Null我只想要一行4 5可以有人帮我怎样做这个sql server吗?

1 个答案:

答案 0 :(得分: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