SQL order by highest value of two columns

postgresql, sql

Solution

SELECT  *
FROM    mytable
ORDER BY
        GREATEST(pc_1, pc_2) DESC, LEAST(pc_1, pc_2) DESC

Problem

I have a PostgreSQL table with some doubles, they store precentages. so let's say the columns are `pc_1` and `pc_2`. What I want is to order by whichever of these two columns has the highest amount descending, and then by the other column, again descending. So if our data is as follows: ``` id pc_1 pc_2 1 12.5 11.0 2 10.0 13.2 3 13.2 9.0 ``` ``` select * from mytable order by <something> ``` Would give: ``` 2 10.0 13.2 3 13.2 9.0 1 12.5 11.0 ```

Original source