Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting in postgresql with different criteria to each column in case of tie

Tags:

I have a problem with sorting in postgresql. The information must be sorted in descending order according to the first column, but in case of a tie, the information must be sorted in ascending order according to the second column.

How can I accomplish this ?

like image 632
user2233540 Avatar asked Apr 14 '13 22:04

user2233540


People also ask

Can we sort multiple columns by ORDER BY clause in a single query?

You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.

How do I sort multiple columns in PostgreSQL?

If you want to sort the result set based on multiple columns or expressions, you need to place a comma ( , ) between two columns or expressions to separate them. Second, you use the ASC option to sort rows in ascending order and the DESC option to sort rows in descending order.

Can you arrange the result set of an SQL query on multiple columns?

Can you arrange the result set of an SQL query on multiple columns? If you specify multiple columns, the result set is sorted by the first column and then that sorted result set is sorted by the second column, and so on.


1 Answers

SELECT ... ORDER BY col1 DESC, col2 ASC

like image 197
Dondi Michael Stroma Avatar answered Oct 02 '22 12:10

Dondi Michael Stroma