Home > OS >  Custom sorting in Amazon Redshift / SQL
Custom sorting in Amazon Redshift / SQL

Time:05-11

I'm writing some SQL queries where I need to sort the grouped values by abbreviated month names. I think I've tried all the possibilities I could found but still something is not working. I try to use the 'ORDER BY CASE' statement which was advised here but that doesn't work and all the time I get this error:

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

Here is an example of my query.

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

I'm connecting to the Redshift via Tableau Online. Thanks in advance!

Update:

CodePudding user response:

I would rather use the month-number-to-month-name conversion at the very end. Something like this:

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
  • Related