2017-02-25 55 views
0

我嘗試獲取結果3,5沒有計數重複,NUll值和空數據。SQL plus 2列數據內部計數

select count(distinct no1), count(distinct no1) + count(distinct no2) from abc where no1 
    is not null 

enter image description here

enter image description here

+0

我不想在計數中包含空值和空值。想要結果3 5 –

回答

1

如果你不想空,空,試試這個:

select 
    count(distinct case when no1 = '' then null else no1 end), 
    count(distinct case when no1 = '' then null else no1 end) 
    + count(distinct case when no2 = '' then null else no2 end) 
from `tbl` 

Demo在這裏。

解釋countsumavg等等......像這些聚合函數不會採取null作爲其的測算對象。所以只需將空值設爲null,這裏使用case when來做到這一點,那麼count distinct就會得到你想要的。

+0

它的工作原理。謝謝 –

1
select count(distinct no1), count(distinct no1) + (select count(distinct no2) from abc where no2 is not null and no2<>'') from abc where no1 is not null 

試試這個

+0

不起作用,它出現錯誤「關鍵字附近的語法不正確」選擇「。」和「附近語法不正確」)「。」 –

+0

現在我改變了查詢再試 –

+0

這項工作也。謝謝 –