user2232273
user2232273

Reputation: 4974

SQL - Select * From Table Order By

i have a table with values and what i try to is to select all but show in a different order .

i don´t want to add another column to define the display order,

how can i do that..

i have read about using case.

but i have no success, hope that someone can help with this.

here is my trying code:

My table looks like this:

 1 -- a
 2 -- b
 3 -- bgin
 4 -- mid 
 5 -- c
 6 -- d

and i my result i like to have are this:

 1 -- bgin
 2 -- a
 3 -- b
 4 -- c 
 5 -- d
 6 -- mid


Select Name From tbl
GROUP BY Name
ORDER BY 
CASE WHEN Name = 'Bgin' THEN 0 END, Name,
CASE WHEN Name= 'Mid' THEN 5 END, Name

Upvotes: 2

Views: 1886

Answers (1)

Filipe Silva
Filipe Silva

Reputation: 21667

Try this:

Select Name From tbl
GROUP BY Name
ORDER BY 
CASE WHEN Name = 'Bgin' THEN 0
     WHEN Name = 'Mid' THEN 5
     WHEN Name <> 'Mid' AND Name <> 'Bgin' THEN 1 END, Name

sqlfiddle demo

Upvotes: 4

Related Questions