2017-02-25 3 views
0

私は、カウントの重複なし、NUllの値と空のデータなしの結果3,5を取得しようとします。SQLプラス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

nullと空のcountを含めたくありません。結果が欲しい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 

あなたがnull、空たくない場合は、この

+0

動作していない、「キーワード 'select'の近くに不正な構文があります。」 「近くに構文が正しくない」)」 –

+0

今質問を変更しました。 –

+0

この作業もあります。ありがとう –