From: Scott Marlowe <smarlowe@g2switchworks.com>
To: Volkan YAZICI <volkan.yazici@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Combining two SELECTs by same filters
Date: Wed, 26 Oct 2005 10:59:05 -0500
Message-ID: <1130342345.2872.18.camel@state.g2switchworks.com> (raw)
In-Reply-To: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com>
References: <7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com>
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);
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: smarlowe@g2switchworks.com, volkan.yazici@gmail.com
Subject: Re: Combining two SELECTs by same filters
In-Reply-To: <1130342345.2872.18.camel@state.g2switchworks.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