Re: subquery/alias question

Поиск
Список
Период
Сортировка
От Alvaro Herrera
Тема Re: subquery/alias question
Дата
Msg-id 20070925223048.GN6440@alvh.no-ip.org
обсуждение исходный текст
Ответ на Re: subquery/alias question  (Michael Glaesemann <grzm@seespotcode.net>)
Ответы Re: subquery/alias question  (Michael Glaesemann <grzm@seespotcode.net>)
Список pgsql-general
Michael Glaesemann wrote:
>
> On Sep 25, 2007, at 16:59 , Madison Kelly wrote:
>
>> SELECT
>>     d.dom_id,
>>     d.dom_name,
>>     (SELECT COUNT(*) FROM users u WHERE u.usr_dom_id=d.dom_id)
>>     AS
>>     usr_count
>> FROM
>>     domains d
>> WHERE
>>     (SELECT COUNT(*) FROM users u WHERE u.usr_dom_id=d.dom_id) > 0
>> ORDER BY d.dom_name ASC;
>
> Why not just use a join? Something like this would work, I should think:
>
> select dom_id,
>        dom_name,
>        usr_count
>   from domains
>   natural join (select usr_dom_id as dom_id,
>                        count(usr_dom_id) as usr_count
>                   from users) u
>   where usr_count > 0
>   order by dom_name;

Maybe the usr_count should be tested in a HAVING clause instead of
WHERE?  And put the count(*) in the result list instead of a subselect.
That feels more natural to me anyway.

--
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

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

Предыдущее
От: Greg Smith
Дата:
Сообщение: Re: lowering impact of checkpoints
Следующее
От: Michael Glaesemann
Дата:
Сообщение: Re: subquery/alias question