Re: Combining two SELECTs by same filters - Mailing list pgsql-sql

From Scott Marlowe
Subject Re: Combining two SELECTs by same filters
Date
Msg-id 1130342345.2872.18.camel@state.g2switchworks.com
Whole thread Raw
In response to Combining two SELECTs by same filters  (Volkan YAZICI <volkan.yazici@gmail.com>)
List pgsql-sql
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);


pgsql-sql by date:

Previous
From: Gary Stainburn
Date:
Subject: Re: select best price
Next
From: Jan Wieck
Date:
Subject: Re: why vacuum