Re: Alternative to MS Access Last() function - Mailing list pgsql-novice

From Michael Fuhr
Subject Re: Alternative to MS Access Last() function
Date
Msg-id 20060301192518.GA81805@winnie.fuhr.org
Whole thread Raw
In response to Re: Alternative to MS Access Last() function  ("Scott Ford" <Scott.Ford@bullfrogpower.com>)
List pgsql-novice
On Wed, Mar 01, 2006 at 01:31:28PM -0500, Scott Ford wrote:
> The LAST function returns the value of the last record in the specified
> field.

The term "last record" is ambiguous.  In SQL a table has no particular
order; queries declare the order they want with an ORDER BY clause.
Which record will be "first" or "last" depends on what order the
query specifies.

> Example:
> SELECT LAST(Age) AS highest_age
> FROM Persons
> ORDER BY Age
> --------------
>
> So I think that I should just be able to use MAX().  Right?

For this example, yes, MAX looks equivalent.

  SELECT MAX(age) AS highest_age FROM persons;

Another way to write this query is:

  SELECT age AS highest_age FROM persons ORDER BY age DESC LIMIT 1;

In other words, order the table by age (descending) and return only
the first row (LIMIT 1) of the resulting row set.  In versions of
PostgreSQL prior to 8.1 the second form is generally faster if the
table has an index on age (a lot faster if the table is large).

--
Michael Fuhr

pgsql-novice by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: Alternative to MS Access Last() function
Next
From: Michael Fuhr
Date:
Subject: Re: delay of function