Group By but include “missing” values

前端 未结 4 2060
醉梦人生
醉梦人生 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:40

    Try this:

    SELECT  b.Color, 
                    sum(fann_sales) 
     FROM   (
                    SELECT  case
                                        when fcompany = 'Acme' then 'Red'
                                        when fcompany = 'Acme Rockets' then 'Blue'
                                        else 'Green'
                                    end
                                    Color,
                                    fann_sales
                        FROM slcdpm
                ) a  RIGHT JOIN 
                (
                    SELECT 'Red' AS Color
                    UNION ALL
                    SELECT 'Blue' AS Color
                    UNION ALL
                    SELECT 'Green' AS Color
                ) b
            ON a.Color = b.Color            
     GROUP BY  b.Color
    

提交回复
热议问题