クエリ書いてみた

元ネタ

というわけで書いてみた。

with [左] ([VAL]) as (
  select 1 union all select 2
), [右] ([VAL]) as ( 
  select 1 union all select 1 union all select 1 union all 
  select 3 union all select 3 union all 
  select 4 
)
select isnull([左].[VAL], -1), count([右].[VAL]) 
from [左] full outer join [右] on ( [左].[VAL] = [右].[VAL] )
group by [左].[VAL]

結果

グルーピングせずに、full outer join した結果は、こんなの

count は null のデータは数えないので左が 2 のデータは 0 件になります。

他の方が書かれたクエリ*1を見て COALESCE (Transact-SQL) を知るなど。これは Default 値という意図を伝えやすいくて良いなー。

*1:[http://ideone.com/Atd0q]