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

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -