Re: DIfference between max() and greatest() ? - Mailing list pgsql-sql

From Ruben Gouveia
Subject Re: DIfference between max() and greatest() ?
Date
Msg-id 51e507b0809120947s51d918e6md19261b7bffa1cf1@mail.gmail.com
Whole thread Raw
In response to Re: DIfference between max() and greatest() ?  ("Bart Degryse" <Bart.Degryse@indicator.be>)
List pgsql-sql
Thanks Bart,

Your explanation is great. Returned and Bought are of datatypes date, hence Returned would be a more recent value.



On Fri, Sep 12, 2008 at 12:10 AM, Bart Degryse <Bart.Degryse@indicator.be> wrote:
max(expression) is an aggregate function
(from the manual: "maximum value of expression across all input values")
greatest(value [, ...]) is not an aggregate function
(from the manual: "The GREATEST function selects the largest value from a list of any number of expressions.")
 
So max takes the maximum of values coming from several rows, while greatest takes the maximum of values coming from 1 row.
 
Assuming
select model,count(distinct cars)
from rc_cars
group by model
returns more than one record you will need both functions.
Greatest gets the most recent date out of "bought" and "returned" PER RECORD.
Max gets the most recent date out of all these greatest dates OVER ALL RECORDS. 
 
 
ModelBoughtReturned
X2004-08-252005-01-01->Greatest = 2005-01-01
X2006-02-172006-02-18->Greatest = 2006-02-18
X2005-11-132001-05-16->Greatest = 2005-11-13
¯
Max = 2006-02-18
 
That being said, isn't it unlikely that "bought" is more recent than "returned"?
I can imagine that one can only return a car after buying it.
If so, writing
... WHERE max(returned) < current_date - interval '1 day' ...
 would be enough.
Another thought: with this WHERE clause a car returned yesterday will not show up.
Is that what you want? If not, use
... WHERE max(returned) < current_date ...
 
Good luck


>>> "Ruben Gouveia" rubes7202@gmail.com> 2008-09-11 19:33 >>
What is the difference between these two. I know that max() is an aggregate function

select model,count(distinct cars)
from rc_cars
where greatest(baught,returned) < current_date - integer '1'
group by model;

Do i need to have a max () around a greatest() to make sure i get the most recent of the two.

select model,count(distinct cars)
from rc_cars
where max(greatest(baught,returned)) < current_date - integer '1'
group by model;



pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Pls Hlp: SQL Problem
Next
From: "Rafael Domiciano"
Date:
Subject: Doubts about FK