Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XJRZ0-0003Ue-9s for pgsql-admin@arkaria.postgresql.org; Mon, 18 Aug 2014 18:21:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XJRYz-0003SC-Lo for pgsql-admin@arkaria.postgresql.org; Mon, 18 Aug 2014 18:21:49 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XJRYy-0003S6-QL for pgsql-admin@postgresql.org; Mon, 18 Aug 2014 18:21:48 +0000 Received: from fep13.mx.upcmail.net ([62.179.121.33]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XJRYt-0001bd-TJ for pgsql-admin@postgresql.org; Mon, 18 Aug 2014 18:21:46 +0000 Received: from edge03.upcmail.net ([192.168.13.238]) by viefep13-int.chello.at (InterMail vM.8.01.05.05 201-2260-151-110-20120111) with ESMTP id <20140818182139.OCKP23223.viefep13-int.chello.at@edge03.upcmail.net>; Mon, 18 Aug 2014 20:21:39 +0200 Received: from [192.168.1.160] ([86.101.30.243]) by edge03.upcmail.net with edge id gJMf1o00T5EjbRA03JMfxk; Mon, 18 Aug 2014 20:21:39 +0200 X-SourceIP: 86.101.30.243 Message-ID: <53F24431.9030801@shopzeus.com> Date: Mon, 18 Aug 2014 20:21:37 +0200 From: =?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?= User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: sum, min, max and null Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.0 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org Hi, I understand that sum, min and max ignore null values. So for example this query: select sum(v),min(v) from ( select 1 as v union all select null union all select 3 ) s will result in sum(v)=4 , min(v)=1. However, I'm in need of a different interpretation, where sum() should change the output value to null if there is any NULL value in the input. How do I achieve this? I was experimenting with this: select case when sum(hasnull)>0 then null else sum(v) end as mysum from ( select v, case when v is null then 1 else 0 end as hasnull from ( select 1 as v union all select null union all select 3 ) s ) s2 It works but this is so ugly that I'm not even trying to use it in production code. Is there any better way to do it? Thanks, Laszlo -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin