Combining two SELECTs by same filters

Поиск
Список
Период
Сортировка
От Volkan YAZICI
Тема Combining two SELECTs by same filters
Дата
Msg-id 7104a7370510260816l9262d19j808d23c023e8445e@mail.gmail.com
обсуждение исходный текст
Ответы Re: Combining two SELECTs by same filters  (Scott Marlowe <smarlowe@g2switchworks.com>)
Re: Combining two SELECTs by same filters  (Michael Fuhr <mike@fuhr.org>)
Re: Combining two SELECTs by same filters  (Bruno Wolff III <bruno@wolff.to>)
Список pgsql-sql
Hi,

I've a table like:

=> SELECT dt FROM sales WHERE id = 2;          dt
----------------------------2005-10-25 21:43:35.8700492005-10-25 21:43:36.2541222005-10-25 21:43:36.5911962005-10-25
21:43:36.8933312005-10-2521:43:37.2656712005-10-25 21:43:37.6881862005-10-25 22:25:35.2131712005-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.


В списке pgsql-sql по дате отправления:

Предыдущее
От: Stephan Szabo
Дата:
Сообщение: Re: RETURNS SETOF primitive returns results in parentheses
Следующее
От: Gary Stainburn
Дата:
Сообщение: select best price