pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Volkan YAZICI <volkan.yazici@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Combining two SELECTs by same filters
Date: Wed, 26 Oct 2005 18:16:13 +0300
Message-ID: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com> (raw)

Hi,

I've a table like:

=> SELECT dt FROM sales WHERE id = 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 = 21 and hour = 22.
For this purpose, I'm using below SELECT query:

=> 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.

Regards.



view thread (4+ messages)  latest in thread

Message-ID: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com>
Permalink:  ../7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com/
Also on:    postgresql.org/message-id/7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: volkan.yazici@gmail.com
  Subject: Re: Combining two SELECTs by same filters
  In-Reply-To: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox