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 A300CDAA55 for ; Wed, 26 Oct 2005 13:16:08 -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 76200-08 for ; Wed, 26 Oct 2005 16:16:05 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 32F1FDA8D2 for ; Wed, 26 Oct 2005 13:16:04 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j9QGFxsJ004567 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 26 Oct 2005 10:16:02 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id j9QGFxCw045031; Wed, 26 Oct 2005 10:15:59 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id j9QGFxkS045030; Wed, 26 Oct 2005 10:15:59 -0600 (MDT) (envelope-from mfuhr) Date: Wed, 26 Oct 2005 10:15:58 -0600 From: Michael Fuhr To: Volkan YAZICI Cc: pgsql-sql@postgresql.org Subject: Re: Combining two SELECTs by same filters Message-ID: <20051026161558.GA44965@winnie.fuhr.org> References: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200510/337 X-Sequence-Number: 23190 On Wed, Oct 26, 2005 at 06:16:13PM +0300, 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. If you can accept multiple rows instead of multiple columns then one way would be to group by the hour: SELECT date_trunc('hour', dt) AS hour, count(*) FROM sales WHERE id = 2 AND date_trunc('hour', dt) IN ('2005-10-25 21:00:00', '2005-10-25 22:00:00') GROUP BY hour ORDER BY hour; hour | count ---------------------+------- 2005-10-25 21:00:00 | 6 2005-10-25 22:00:00 | 2 (2 rows) Here's another possibility, but I find it a bit ugly: SELECT sum(CASE date_trunc('hour', dt) WHEN '2005-10-25 21:00:00' THEN 1 ELSE 0 END) AS count1, sum(CASE date_trunc('hour', dt) WHEN '2005-10-25 22:00:00' THEN 1 ELSE 0 END) AS count2 FROM sales WHERE id = 2; count1 | count2 --------+-------- 6 | 2 (1 row) If you're looking for the fastest method then use EXPLAIN ANALYZE on each to see what works best on your data set. -- Michael Fuhr