Skip site navigation (1) Skip section navigation (2)

How to find the max value in a select?

From: Erich <hh(at)cyberpass(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: How to find the max value in a select?
Date: 2000-07-31 06:17:07
Message-ID: 200007310617.XAA02112@cyberpass.net (view raw or flat)
Thread:
Lists: pgsql-general
I need to do something like this:

SELECT ...
FROM ...
WHERE ...
ORDER BY ...
COUNT 1

In other words, I want to find the one row matched by my WHERE clause
which is the maximum or minimum of all the rows that matched it.  I
could do the query above (ORDER BY...   COUNT 1), but does Postgres
optimize this, or does it find all the rows, sort them, and then take
off the top one?  Or is there some better way to do it?

Thanks,

e

Responses

pgsql-general by date

Next:From: Prasanth A. KumarDate: 2000-07-31 06:30:06
Subject: Re: How to find the max value in a select?
Previous:From: Oliver ElphickDate: 2000-07-31 05:40:08
Subject: Re: Inheritance

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group