I have a request, and I would like to make two types of orders based on the condition.
For example, if the field is NULL, I need to place an order, and if not, I have to place another order. How can i do this?
select *
from table_1 t
order by (if t.field1 is null then
order by t.field2 DESC, field3 ASC
else
order by t.field4 ASC, field5 DESC)
This is an example code: I want to do a different order (ASC / DESC and different columns) based on the value of FIELD1
Example
CONDITIONAL
ID FIELD1 FIELD2 FIELD3 FIELD4 FIELD5
1 1 2 3 4 5
2 NULL 6 7 8 9
DATA
ID PARENT_ID DATA1 DATA2 DATA3
1 1 X Y J
2 1 Z W U
3 2 XY YX O
4 2 ZW WZ I
select d.*
from data d, conditional c
where d.parent_id = c.id
and d.parent_id = 1
order by
case
when c.field1 is null then
data1 asc, data2 desc
else
data3 asc, data1 desc
end
In this example, I select the DATA ONE and TWO rows (rows with parent id = 1). Now that I have made this decision, I want DATA columns or columns to be based on the value of the CONDICTIONAL.FIELD1 column. Hopefully it will be cleaner now.
I am sure that this request works, but it is "what I need."
source
share