如何在Count()中指定条件


How to specify condition in Count()?

我只想计算具有status=0的行示例a_no列具有status=0及其计数作为一个

这是我的查询

select date, count(DISTINCT no_a) as Transaction, 
     CASE when status=0 then count(distinct no_a) end as Success
     from transfer_tx_201503

您可以指定计数之外的条件:

select 
    count(distinct no_a) as Transaction,
    count(distinct case status when 0 then no_a else -1 end case) - 1 as Success
from your_table

假设no_a是整数,并且值-1不是有效值,这应该会起作用。您只需将所有状态为非零的行放在一个bucket中,然后从计数中减去一。