Problems using count() with a join

From: "Igor Kryltsov" <kryltsov(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Problems using count() with a join
Date: 2004-08-17 02:31:21
Message-ID: cfrqke$1l1k$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,I am using slightly modified example posted by Doug Younger and answered
by Tom Lane
:)(http://archives.postgresql.org/pgsql-sql/1999-08/msg00159.php)
I have the following 2 tables:

Table groups:
g_id int4
g_name text

Table users:
u_id int4
g_id int4 u_act int4 0 - value means "Inactive" and 1 - value
means "Active" (used instead of boolean type for DB interoperability :) )
What I want is to get a count of users in each group with count of active
users in each group, even if there are no users in the group.

This example gives a count of users in each group:
SELECT t1.g_name,count(t2.g_id) as users_count
FROM groups t1,users t2
WHERE t1.g_id = t2.g_id
GROUP BY t1.g_name;If you can help to modify it to output --> g_name,
users_count, active_users_count So it could be:Group_A | 89 |
34Group_B | 75 | 75Group_C | 25 | 0 <-- all users
are inactive hereGroup_D | 0 | 0 <---- Assume that this is a
result of UNION which will add groups without employeesThank you,Igor

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Igor Kryltsov 2004-08-17 02:34:55 Re: Problems using count() with a join - trying to format it better
Previous Message Glen Parker 2004-08-17 01:08:13 Re: pg_clog and pg_xlog empty, postgresql refuses to start