X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 85910D70EE for ; Mon, 22 Aug 2005 17:29:07 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 35990-02 for ; Mon, 22 Aug 2005 20:29:05 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id D0CA5D70E7 for ; Mon, 22 Aug 2005 17:29:04 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id A04083542D; Mon, 22 Aug 2005 13:29:05 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 9E7ED353F7; Mon, 22 Aug 2005 13:29:05 -0700 (PDT) Date: Mon, 22 Aug 2005 13:29:05 -0700 (PDT) From: Stephan Szabo To: Lane Van Ingen Cc: pgsql-sql@postgresql.org Subject: Re: Why Doesn't SQL This Expression Work? In-Reply-To: Message-ID: <20050822132441.P16438@megazone.bigpanda.com> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 required=5 tests=[AWL=0.005] X-Spam-Level: X-Archive-Number: 200508/192 X-Sequence-Number: 22420 On Mon, 22 Aug 2005, Lane Van Ingen wrote: > Hi, am trying to do a simple computation on two views, but for some reason > the current_util_in computation always returns zero. All fields being used > are integer. > > select a.if_id, > a.in_count, > a.time_incr, > b.speed, > ((a.time_incr * b.speed) / 8) as possible_bytes, > (a.in_count / ((a.time_incr * b.speed) / 8) * 100) AS current_util_in, > from if_history_view1 a, speed_history_view1 b > where a.if_id = b.if_id > and a.if_id = 2; > > The inner computation (a.time_incr * b.speed / 8) evaluated properly to > 7675200. > Add the "in_count divide operation", and the result is zero. Integer division doesn't follow all the same rules as normal division would. In particular (a/b)*c is not the same as a*c/b. Also, I think you may be expecting rounding rather than truncation (and in the case of (a.time_incr*b.speed)/8 can that not be a multiple of 8, and if so what should happen?)