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 49396DAF0C for ; Wed, 26 Oct 2005 12:59:09 -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 70548-08 for ; Wed, 26 Oct 2005 15:59:07 +0000 (GMT) Received: from koolancexeon.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by svr1.postgresql.org (Postfix) with ESMTP id 22E71DAEF6 for ; Wed, 26 Oct 2005 12:59:06 -0300 (ADT) Received: mail.g2switchworks.com 10.10.1.8 from 10.10.1.37 10.10.1.37 via HTTP with MS-WebStorage 6.5.6944 Received: from state.g2switchworks.com by mail.g2switchworks.com; 26 Oct 2005 10:59:05 -0500 Subject: Re: Combining two SELECTs by same filters From: Scott Marlowe To: Volkan YAZICI Cc: pgsql-sql@postgresql.org In-Reply-To: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com> References: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1130342345.2872.18.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Wed, 26 Oct 2005 10:59:05 -0500 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 required=5 tests=[AWL=0.007] X-Spam-Level: X-Archive-Number: 200510/332 X-Sequence-Number: 23185 On Wed, 2005-10-26 at 10:16, Volkan YAZICI wrote: > => SELECT > -> (SELECT count(id) FROM sales > -> WHERE id = 2 > -> AND date_trunc('hour', dt) = '2005-10-25 21:00:00'), > -> (SELECT count(id) FROM sales > -> WHERE id = 2 > -> AND date_trunc('hour', dt) = '2005-10-25 22:00:00'); > ?column? | ?column? > ----------+---------- > 6 | 2 > (1 row) > > Isn't it possible to combine these two SELECTs as one. Because one of > their filters are same: id = 2. I'm just trying to avoid making 2 > scans with nearly same filters. Do something like this: select count(id) from sales where id=2 and dt between 'firstdatehere' and 'lastdatehere' group by date_trunc('hour', dt);