Suppose I have the following.
select
case
when fcompany = 'Acme' then 'Red'
when fcompany = 'Acme Rockets' then 'Blue'
else 'Green'
end
Color
,sum(fann_sales)
FROM
slcdpm
group by
case
when fcompany = 'Acme' then 'Red'
when fcompany = 'Acme Rockets' then 'Blue'
else 'Green'
end
Let's say it often returns with only two colors. What's the best way to pull all three colors and include 0 for the missing value?
Union All?
If all colors must be present, why not present them as columns?
Otherwise, just go with @JohnK813 answer.
Yes, Union All may be your best bet.
Try this:
Move the GROUP into a conditional SUM with more columns?
One pass over the table for this. A UNION ALL or subquery approach (in other answers) will touch the table once per clause = somewhat slower.