X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 9362C11F6CDC for ; Sat, 1 Apr 2006 02:32:44 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 87598-02 for ; Sat, 1 Apr 2006 02:32:42 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 9769C11F6CDA for ; Sat, 1 Apr 2006 02:32:40 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k316Wdg0008348; Sat, 1 Apr 2006 01:32:39 -0500 (EST) To: vince@weaktight.com cc: pgsql-sql@postgresql.org Subject: Re: query; check for zero and floats In-reply-to: <20060401010317.tbza82opw0c0ccg0@webmail.weaktight.com> References: <20060401010317.tbza82opw0c0ccg0@webmail.weaktight.com> Comments: In-reply-to vince@weaktight.com message dated "Sat, 01 Apr 2006 01:03:17 -0500" Date: Sat, 01 Apr 2006 01:32:39 -0500 Message-ID: <8347.1143873159@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.114 required=5 tests=[AWL=0.114] X-Spam-Score: 0.114 X-Spam-Level: X-Archive-Number: 200604/2 X-Sequence-Number: 24811 vince@weaktight.com writes: > I'm trying to do a simple query and I'm not sure how to get it to work: > SELECT SUM(x0 + y0 + z0) / SUM(x2 + y2) AS A1, SUM(x1 + y1 + z1) / SUM(x3 + y3) > AS A2 > FROM test > 1. All variables are integers. When it does the division, it returns an > integer, but I want a float. (I've tried numerous things to no avail) Cast one or both division inputs to float. > 2. If SUM(x2 + y2) == 0; there is a divide by zero error. How do I check and > pass on zeroes? Add a HAVING condition (not WHERE, because you need to filter on the post-aggregation status). I think you want SELECT SUM(x0 + y0 + z0)::float / SUM(x2 + y2) AS A1, SUM(x1 + y1 + z1)::float / SUM(x3 + y3) AS A2 FROM test HAVING SUM(x2 + y2) <> 0 (maybe also having SUM(x3 + y3) <> 0) regards, tom lane