Re: SELECT MAX returns wrong value

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Gavin Baumanis" <gavinb(at)eclinic(dot)com(dot)au>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SELECT MAX returns wrong value
Date: 2007-12-14 17:24:14
Message-ID: dcc563d10712140924h758ec6dep8129f9ef71968df9@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Dec 13, 2007 5:09 PM, Gavin Baumanis <gavinb(at)eclinic(dot)com(dot)au> wrote:
> Hi Everyone,
>
> Sorry if I am missing something obvious but I think I have found a bug.
> If I perform the following SQL
>
> SELECT MAX(column) FROM table WHERE expression
>
> and there is no match, Postgres returns a record count of 1.
> There is no value in max, it is NULL.

that's because you got one record back. A null one, but a record none the less.

The standard way of doing this is:

select count(column) from table where expression.

since null columns don't get counted, it will return zero if they're all null.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Oleg Kharin 2007-12-15 15:15:45 Bad count of rows estimated for emerge join
Previous Message Tom Lane 2007-12-14 17:17:42 Re: odd error updating - varchar