Amazon Redshift 中的自定义排序 / SQL

Custom sorting in Amazon Redshift / SQL

我正在编写一些 SQL 查询,我需要在其中按缩写的月份名称对分组值进行排序。我想我已经尝试了我能找到的所有可能性,但仍然无法正常工作。我尝试使用建议 的 'ORDER BY CASE' 语句,但这不起作用,并且我一直收到此错误:

Request Rejected by Server TableauException: [Amazon][Amazon Redshift] (30) Error occurred while trying to execute a query: [SQLState 42703] ERROR: column "months_strings" does not exist in vw_rs_incident_service_level_results

这是我的查询示例。

with filtered_table as (
    select slmdefinition, state, 
    breachedflag, mesurementstopdatelocal, cgaccount,
    case
      when extract(month from mesurementstopdatelocal) = 1 then 'Jan'
      when extract(month from mesurementstopdatelocal) = 2 then 'Feb'
      when extract(month from mesurementstopdatelocal) = 3 then 'Mar'
      when extract(month from mesurementstopdatelocal) = 4 then 'Apr' 
      when extract(month from mesurementstopdatelocal) = 5 then 'May'
      when extract(month from mesurementstopdatelocal) = 6 then 'Jun'
      when extract(month from mesurementstopdatelocal) = 7 then 'Jul'
      when extract(month from mesurementstopdatelocal) = 8 then 'Aug'
      when extract(month from mesurementstopdatelocal) = 9 then 'Sep'
      when extract(month from mesurementstopdatelocal) = 10 then 'Oct' 
      when extract(month from mesurementstopdatelocal) = 11 then 'Nov'
      when extract(month from mesurementstopdatelocal) = 12 then 'Dec'  
    end as months_strings
    from cdm_explorer_schema.vw_rs_incident_service_level_results
    where (state = 'completed' and slmdefinition like '%Resolution%' and 
    slmdefinition like 'L%')
    )

    select slmdefinition as slm_definition,
        sum(case when breachedflag = 'false' then 1 else 0 end) as 
        number_of_unbreachflaged,
        count(breachedflag) as number_of_all_incidents,
        number_of_unbreachflaged / number_of_all_incidents * 100 as ratio,
        months_strings as month,
        cgaccount as company_name
        from filtered_table
        group by company_name, slm_definition, month
        order by case
            when month = 'Jan' then 1
            when month = 'Feb' then 2
            when month = 'Mar' then 3
            when month = 'Apr' then 4
            when month = 'May' then 5
            end

我正在通过 Tableau Online 连接到 Redshift。提前致谢!

更新:

我宁愿在最后使用 month-number-to-month-name 转换。像这样:

with filtered_table as (
    select foo, bar, extract(month from mesurementstopdatelocal) as month_number
    from ...
    where ...
)
select foo, bar, case month_number
    when 1 then 'Jan'
    when 2 then 'Feb'
    when 12 then 'Dec'
end as month_name
from filtered_table
group by foo, bar, month_number
order by month_number