Re: SELECT MAX returns wrong value - Mailing list pgsql-sql

From Scott Marlowe
Subject Re: SELECT MAX returns wrong value
Date
Msg-id dcc563d10712140924h758ec6dep8129f9ef71968df9@mail.gmail.com
Whole thread Raw
In response to SELECT MAX returns wrong value  (Gavin Baumanis <gavinb@eclinic.com.au>)
List pgsql-sql
On Dec 13, 2007 5:09 PM, Gavin Baumanis <gavinb@eclinic.com.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.


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: odd error updating - varchar
Next
From: "Oleg Kharin"
Date:
Subject: Bad count of rows estimated for emerge join