Return the query in a particular format

  • Here is my query I need to get All in the first row and the remaining in the alphabetical order

    select distinct State_Code,

    case when State_Code=' '

    then 'Unknown'

    else State_Code

    end State_Code2,

    1 as 'State_Code1'

    from Dim_Prop_Geo_T

    union

    select 'All' as State_Code, 'All' ,0 as 'orderby'

    order by 'State_Code2'

    This query returns in alphabetical order .....I need all in the first row

  • This was removed by the editor as SPAM

  • You need CASE with ORDER BY clause, try this:

    with cte as (

    select distinct

    State_Code,

    case when State_Code=' ' then 'Unknown'

    else State_Code

    end State_Code2,

    1 as 'State_Code1'

    from Dim_Prop_Geo_T

    union

    select 'All' as State_Code,

    'All' ,

    0 as 'orderby')

    select State_Code, State_Code2, State_Code1

    from cte

    order by case when State_Code='All' then 0 else 1 end

    ... you may need to tweak the query for correct output.

    For more info on using CASE with ORDER BY clause check this link: http://sqlwithmanoj.wordpress.com/?s=order+by+case

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply