Order By - Leave specific record by first

0
Store_Name  | Sales | Date
America     | 1500  | 05-Jan-1999
Boston      | 700   | 08-Jan-1999
Canada      | 300   | 08-Jan-1999
Dinamarca   | 250   | 07-Jan-1999 

I have this table, I would like to sort it in a way where Boston would be the first record, the order of the others being irrelevant. How do I?

    
asked by anonymous 23.07.2014 / 15:49

3 answers

6

You do not need to use case

select *
from t
order by store_name != 'Boston'

The false commands before the true. And you can do a normal sort with the other lines:

order by store_name != 'Boston', store_name
    
30.09.2014 / 13:48
3
select *
from tabela
order by case when store_name = 'Boston' then 1 else 2 end;
    
23.07.2014 / 15:55
1

I prefer an alternative in which I do not need to create a dynamic order by

select Store_Name, Sales, Date, case when store_name = 'Boston' then 0 else 9 end as Orderby
from tabela
order by orderby

In addition to this alternative you can add an orderby column in the table, hence it is easy to place more than one record with a "fixed" sort.

I believe that the solution using a case in the order by clause can generate query plans that are not very "legal" (this needs to be tested beforehand!)

Here's a fiddle for reference

    
23.07.2014 / 16:29