2015-04-06 90 views
0

我只想计算有行status=0 例如a_no专栏status=0,其计为一个如何在Count()中指定条件?

这是我的查询

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

你为什么不使用简单的where子句? – 2015-04-06 06:50:37

+0

如果我使用的地方它也过滤交易,即我想显示所有数字,即使状态!= 0 – 2015-04-06 06:52:26

+0

所以你要计数有状态= 0的行和作为交易 – 2015-04-06 06:54:25

回答

0

,你可以指望以外指定条件:

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不是有效值。 只需将所有具有非零状态的行放入一个存储桶中,然后从计数中减去一个。

+0

谢谢你的回答,但它不是我要找的东西。我只想在成功列中计数状态= 0。如果我使用的交易也将遵循条件 – 2015-04-06 06:55:33

+0

哦,我看到..它不清楚你到底想要什么 – 2015-04-06 06:56:29

+0

这就是你想要的(查看更新) - 计算status = 0的行吗? – 2015-04-06 06:59:32

相关问题