Re: [SQL] Find min and max values across two columns?

2006-05-15 Thread Emi Lu

Hello,

I tried "select greatest(max(a), max(b)) from public.test",  but I got 
the following errors:


ERROR:  function greatest(integer, integer) does not exist
HINT:  No function matches the given name and argument types. You may 
need to add explicit type casts.


May I know where I can read the docs about greatest & least please.

I am using  PostgreSQL 8.0.7.

Thanks,
Ying




Amos Hayes <[EMAIL PROTECTED]> writes:
 

I'm trying to build a query that among other things, returns the  
minimum and maximum values contained in either of two columns.
   



I think you might be looking for

select greatest(max(columnA), max(columnB)) from tab;
select least(min(columnA), min(columnB)) from tab;

greatest/least are relatively new but you can roll your own in
older PG releases.

regards, tom lane

---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
  choose an index scan if your joining column's datatypes do not
  match
 




---(end of broadcast)---
TIP 5: don't forget to increase your free space map settings


Re: [SQL] Find min and max values across two columns?

2006-05-15 Thread Scott Marlowe
On Mon, 2006-05-15 at 16:40, Emi Lu wrote:
> Hello,
> 
> I tried "select greatest(max(a), max(b)) from public.test",  but I got 
> the following errors:
> 
> ERROR:  function greatest(integer, integer) does not exist
> HINT:  No function matches the given name and argument types. You may 
> need to add explicit type casts.
> 
> May I know where I can read the docs about greatest & least please.
> 
> I am using  PostgreSQL 8.0.7.

You need to define your problem better.  Assuming these data:

a  b
12 24
24 12
13 18
25 10

which should I get for greatest(max(a), max(b)) should I get?  Do you
just want 25,24???  In that case you don't need greatest.  Do you need
the highest total between the two, or what?

---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
   choose an index scan if your joining column's datatypes do not
   match


Re: [SQL] Find min and max values across two columns?

2006-05-15 Thread Tom Lane
Emi Lu <[EMAIL PROTECTED]> writes:
> ERROR:  function greatest(integer, integer) does not exist
> HINT:  No function matches the given name and argument types. You may 
> need to add explicit type casts.

We added greatest/least in 8.1, but before that you can just use a
CASE expression instead, along the lines of
case when x>y then x else y end

regards, tom lane

---(end of broadcast)---
TIP 6: explain analyze is your friend