I'm sorry if this is a simple question. However, I have searched high and low for the answer over about an hour.
I have a db_table with that I need to sort for output to a table in a webpage. The data is stored in the db_table in the following way:
date Area Value
------ ------ -------
11-mar-18 middle 10
11-mar-18 bottom 5
11-mar-18 top 12
12-mar-18 top 14
12-mar-18 bottom 4
12-mar-18 middle 17
The question is: how can I sort these to produce the following result:
date Area Value
------ ------ -------
11-mar-18 top 12
11-mar-18 middle 10
11-mar-18 bottom 5
12-mar-18 top 14
12-mar-18 middle 17
12-mar-18 bottom 4
Any help is greatly appreciated.
Sort area descending alphabetically as the second component of order by :
select *
from db_table
order by "date", area desc;
As you mentioned if area has values A,B,C,D and they wanted to be sorted in the order of C,B,D,A, then use :
select *
from db_table
order by "date", decode(area,'C',1,'B',2,'D',3,'A',4);
P.S. especially, i put date column inside quotes, since already created table with "date"
column, instead of date
which is impossible as being a keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With