As part of a database view, I found myself wanting to get Postgres to display values from one of two columns, whichever was the largest.
My first attempt was:
SELECT id, MAX(column1, column2) FROM table1;
which of course didn't work because MAX()
is an aggregate function which only takes a single parameter.
What I was looking for instead, is something that was introduced in 8.1:
SELECT id, GREATEST(column1, column2) FROM table1;
where GREATEST()
(and its opposite: LEAST()
) is a conditional expression.
RSS
Atom
Add a comment