Re: subquery/alias question

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Madison Kelly <linux(at)alteeve(dot)com>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: subquery/alias question
Date: 2007-09-25 22:11:02
Message-ID: B10BFDCA-6C4E-4C0E-A919-95241341C631@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


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;

Michael Glaesemann
grzm seespotcode net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Greg Smith 2007-09-25 22:21:10 Re: lowering impact of checkpoints
Previous Message Scott Marlowe 2007-09-25 22:00:28 Re: Can't connect (2 dbs) or login (2 others)