Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id CCAD3633BA7 for ; Wed, 21 Apr 2010 04:43:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 30122-04 for ; Wed, 21 Apr 2010 07:43:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moon.antora.ru (moon.antora.ru [89.111.189.180]) by mail.postgresql.org (Postfix) with ESMTP id 20C2B633B55 for ; Wed, 21 Apr 2010 04:43:11 -0300 (ADT) Envelope-to: pgsql-sql@postgresql.org Received: from moon.inner.space ([10.1.1.5] helo=moon.antora.ru) by moon.antora.ru with esmtp (Exim 4.67) (envelope-from ) id 1O4UaR-000005-Sk for pgsql-sql@postgresql.org; Wed, 21 Apr 2010 11:43:07 +0400 Received: from [192.168.1.6] [178.34.189.172] by moon.antora.ru (Axigen) with (DHE-RSA-AES256-SHA encrypted) ESMTPSA id 19762B; Wed, 21 Apr 2010 11:43:07 +0400 Message-ID: <4BCEAC8A.80208@bankir.ru> Date: Wed, 21 Apr 2010 11:43:06 +0400 From: silly sad User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5) Gecko/20100111 Thunderbird/3.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: How to max() make null as biggest value? References: <2A8BBCA2-814C-4E8D-BCE1-11FA2F8D120E@gmail.com> In-Reply-To: <2A8BBCA2-814C-4E8D-BCE1-11FA2F8D120E@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Scan-Signature: 518c55fec10a74e2156061d7438613ff X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.274 tagged_above=-10 required=5 tests=AWL=-0.273, BAYES_20=-0.001 X-Spam-Level: X-Archive-Number: 201004/75 X-Sequence-Number: 34002 On 04/14/10 08:33, Feixiong Li wrote: > Hi , guys , > > I am newbie for sql, I have a problem when using max() function, I need > get null when there are null in the value list, or return the largest > value as usual, who can do this? > > i.e. max([1,2,3,4,5]) => 5 > max([1,2,3,4,5,null]) => null if u want a function, not an aggregate then u have the greatest(...) except it does not return null on null input (i was really surprised with this completely perverted behavior (very unusual for postgres), but it is a fact) if u want to cheat u may just coalesce() each input argument then nullif() a result of the greatest() function (if only u have enough space in a reference range to room the one special value instead of null)