Sorting by the maximum value of two columns

From: David Gaudine <davidg(at)alcor(dot)concordia(dot)ca>
To: pgsql-novice(at)postgresql(dot)org
Subject: Sorting by the maximum value of two columns
Date: 2005-10-04 20:59:21
Message-ID: 4342ED29.6070902@alcor.concordia.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I want to use SELECT to view some records sorted (ordered?) by the
maximum value of two fields. I tried

SELECT * FROM mytable ORDER BY MAX(column1,column2)

but there's no such function. How can I do this? Is there a function
for this purpose, or can I write my own, or do I need a different
approach entirely?

I hope what I'm trying to do is clear, but just in case I'll fake an
example.

column1 column2

4 7
9 5
1 6
8 2

MAX(4,7) is 7
MAX(9,5) is 9
MAX(1,6) is 6
MAX(8,2) is 8

Therefore, my SELECT should return
9 5
8 2
4 7
1 6

David

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Jaromír Kamler 2005-10-04 21:06:36 stupid SQL question, how reach different rows of two almost same tables
Previous Message Alvaro Cobo 2005-10-04 20:46:48 Moving from MySQL