Luís Ruivinho
Luís Ruivinho

Reputation: 21

Sort values from a table based on hierarchy values of a field

I would like to know how can i sort values from a table based on hierarchy values of a field.

EX:

A     B 
--------
1     A 
2     F
3     A
4     P
5     O
6     F

I would like sort the values by the B field and appear first the F Values, then A values, then P values and in the end the O values.

In the end, the result must be like this:

2   F
6   F
1   A
3   A
4   P
5   O

Upvotes: 1

Views: 41

Answers (2)

user5683823
user5683823

Reputation:

More compact:

order by translate (B, 'FAPO', '1234')

This will also allow you, if needed (now or in the future) to have PF compared to PA, rather than just single letter values in column B.

Upvotes: 0

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

Use a case expression in order by.

select *
from tablename
order by case when B = 'F' then 1 
              when B = 'A' then 2 
              when B = 'P' then 3 
              when B = 'O' then 4 
          end, A

Upvotes: 4

Related Questions