Answer the question
In order to leave comments, you need to log in
How to sort in SQL?
Hey! I have a table:
id | count | to | status | user_id | created_at | updated_at |
---|---|---|---|---|---|---|
one | 500 | 0 | 0 | one | 2015-02-18 13:54:44.912000 | 2015-02-18 12:54:47.328000 |
2 | 400 | 0 | 2 | one | 2015-02-18 14:04:51.700000 | 2015-02-18 14:04:53.487000 |
Answer the question
In order to leave comments, you need to log in
Use CASE:
select status,
case
when status = 0 then
0
else
1
end as status_tmp ,
updated_at
from your_table t
order by status_tmp , updated_at
desc I use Oracle, but I know CASE is also in PostgreSQL. And it is better to implement checks for the number of lines and the actions following them in a stored procedure.
you need to add a new field of type select IF(status>0,1,0) status1 to the select
and then order by status1, updated_at DESC
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question