tsql - Group By but include "missing" values -
suppose have following.
select case when fcompany = 'acme' 'red' when fcompany = 'acme rockets' 'blue' else 'green' end color ,sum(fann_sales) slcdpm group case when fcompany = 'acme' 'red' when fcompany = 'acme rockets' 'blue' else 'green' end
let's returns 2 colors. what's best way pull 3 colors , include 0 missing value?
union all?
yes, union may best bet.
select 'red' color, sum(fann_sales) slcdpm fcompany = 'acme' group fcompany union select 'blue' color, sum(fann_sales) slcdpm fcompany = 'acme rockets' group fcompany union select 'green' color, sum(fann_sales) slcdpm fcompany <> 'acme' , fcompany <> 'acme rockets' group fcompany
Comments
Post a Comment