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 D0AB3DAABB for ; Wed, 26 Oct 2005 12:16:16 -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 56354-07 for ; Wed, 26 Oct 2005 15:16:14 +0000 (GMT) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.196]) by svr1.postgresql.org (Postfix) with ESMTP id 88DA6DAB65 for ; Wed, 26 Oct 2005 12:16:14 -0300 (ADT) Received: by xproxy.gmail.com with SMTP id s14so319888wxc for ; Wed, 26 Oct 2005 08:16:14 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=YHfS/4Pxi9dFkWenftkD1aHzyk0r4TJ+fYHS2bOrsCKL8aNZ423/kR02niF2vKcWmaQIgkhIRDfI+BAojYFctCKFAEhqOaPyjB1B4kA2yQ5/+YbDL2dgPHRJUrkoAWtgOWtkyTIGx3F+TAKmXcgqxIPWcXZhLjixqd82G8tdzsQ= Received: by 10.65.54.18 with SMTP id g18mr158501qbk; Wed, 26 Oct 2005 08:16:13 -0700 (PDT) Received: by 10.65.115.2 with HTTP; Wed, 26 Oct 2005 08:16:13 -0700 (PDT) Message-ID: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com> Date: Wed, 26 Oct 2005 18:16:13 +0300 From: Volkan YAZICI To: pgsql-sql@postgresql.org Subject: Combining two SELECTs by same filters MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.032 required=5 tests=[AWL=0.008, RCVD_BY_IP=0.024] X-Spam-Level: X-Archive-Number: 200510/328 X-Sequence-Number: 23181 Hi, I've a table like: =3D> SELECT dt FROM sales WHERE id =3D 2; dt ---------------------------- 2005-10-25 21:43:35.870049 2005-10-25 21:43:36.254122 2005-10-25 21:43:36.591196 2005-10-25 21:43:36.893331 2005-10-25 21:43:37.265671 2005-10-25 21:43:37.688186 2005-10-25 22:25:35.213171 2005-10-25 22:25:36.32235 (8 rows) And I want to collect the count of sales at hour =3D 21 and hour =3D 22. For this purpose, I'm using below SELECT query: =3D> SELECT -> (SELECT count(id) FROM sales -> WHERE id =3D 2 -> AND date_trunc('hour', dt) =3D '2005-10-25 21:00:00'), -> (SELECT count(id) FROM sales -> WHERE id =3D 2 -> AND date_trunc('hour', dt) =3D '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 =3D 2. I'm just trying to avoid making 2 scans with nearly same filters. Regards.