Re: Alternative to MS Access Last() function

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: Alternative to MS Access Last() function
Дата
Msg-id 20060301192518.GA81805@winnie.fuhr.org
обсуждение исходный текст
Ответ на Re: Alternative to MS Access Last() function  ("Scott Ford" <Scott.Ford@bullfrogpower.com>)
Список 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 по дате отправления:

Предыдущее
От: Bruno Wolff III
Дата:
Сообщение: Re: Alternative to MS Access Last() function
Следующее
От: Michael Fuhr
Дата:
Сообщение: Re: delay of function