私はSQLServer2005を使用しています。以下のクエリ(実際のクエリから簡略化)を使用すると、次のようになります。
select a,count(distinct b),sum(a) from
(select 1 a,1 b union all
select 2,2 union all
select 2,null union all
select 3,3 union all
select 3,null union all
select 3,null) a
group by a
取得せずに明確なカウントを行う方法はありますか
「警告:NULL値は、集計またはその他のSET操作によって削除されます。」
これが私が考えることができる選択肢です:
- ANSI_WARNINGSをオフにする
2つのクエリに分けます。1つはカウントが異なり、もう1つはnullを削除するwhere句で、もう1つは合計です。
select t1.a, t1.countdistinctb, t2.suma from ( select a,count(distinct b) countdistinctb from ( select 1 a,1 b union all select 2,2 union all select 2,null union all select 3,3 union all select 3,null union all select 3,null ) a where a.b is not null group by a ) t1 left join ( select a,sum(a) suma from ( select 1 a,1 b union all select 2,2 union all select 2,null union all select 3,3 union all select 3,null union all select 3,null ) a group by a ) t2 on t1.a=t2.a
クライアントの警告を無視する
これを行うためのより良い方法はありますか?私はおそらくルート2を下りますが、コードの重複は好きではありません。