2015-04-06 89 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

相關問題