Group By but include “missing” values

前端 未结 4 2049
醉梦人生
醉梦人生 2021-01-25 15:13

Suppose I have the following.

select
  case
    when fcompany = \'Acme\' then \'Red\'
    when fcompany = \'Acme Rockets\' then \'Blue\'
    else \'Green\'
  en         


        
4条回答
  •  孤街浪徒
    2021-01-25 15:51

    Yes, Union All may be your best bet.

    SELECT 'red' AS Color, sum(fann_sales) FROM slcdpm WHERE fcompany = 'Acme' GROUP BY fcompany
    UNION ALL
    SELECT 'blue' AS Color, sum(fann_sales) FROM slcdpm WHERE fcompany = 'Acme Rockets' GROUP BY fcompany
    UNION ALL
    SELECT 'green' AS Color, sum(fann_sales) FROM slcdpm WHERE fcompany <> 'Acme' AND fcompany <> 'Acme Rockets' GROUP BY fcompany
    

提交回复
热议问题