如何使用 case 语句创建视图?

问题描述

我有一张“万圣节”表,其中包含 15 列,其中一个列名为“月”。 如何创建“万圣节”表的视图,其中月份通过 CASE 语句适当地转换为新列 SEASON 进入春季、夏季、秋季和冬季。

示例代码

create view  hallo_welt
  SELECT month,as 'season'
    CASE
     when month=3 or month<=5 then 'Frühling'
     when month=6 or month<=8 then 'sommer'
     when month=9 or month<=11 then 'Herbst'
     when month=12 or month<=2 then 'winter'
     else ''
    END
 from hallowelt;

解决方法

你可能想要类似的东西

create view hallo_welt
  SELECT 
    month,CASE
     when month in (3,4,5) then 'Frühling'
     when month in (6,7,8) then 'sommer'
     when month in (9,10,11) then 'Herbst'
     when month in (12,1,2) then 'winter'
     else null
    END season
 from hallowelt;