472,805 Members | 1,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Problems using count() with a join

Hi,I am using slightly modified example posted by Doug Younger and answered
by Tom Lane
:)(http://archives.postgresql.org/pgsql...8/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
Nov 23 '05 #1
3 2490
Hi,
I am using slightly modified example posted by Doug Younger and answered by
Tom Lane :)
(http://archives.postgresql.org/pgsql...8/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 | 34
Group_B | 75 | 75
Group_C | 25 | 0 <-- all users are inactive here
Group_D | 0 | 0 <---- Assume that this is a result of UNION
which will add groups without employees

Thank you,

Igor
Nov 23 '05 #2
Result can be obtained by:

SELECT g1.g_name,
(select count(*) from users u1 where g1.g_id = u1.g_id) as users_count,
(select count(*) from users u2 where g1.g_id = u2.g_id and u_act = 1) as
Active_users_count
FROM groups g1

Regards,
Igor
"Igor Kryltsov" <kr******@yahoo.com> wrote in message
news:cf***********@news.hub.org...
Hi,
I am using slightly modified example posted by Doug Younger and answered by Tom Lane :)
(http://archives.postgresql.org/pgsql...8/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 | 34
Group_B | 75 | 75
Group_C | 25 | 0 <-- all users are inactive here
Group_D | 0 | 0 <---- Assume that this is a result of UNION
which will add groups without employees

Thank you,

Igor

Nov 23 '05 #3
Hi Igor,

wouldn't

select g_name,count(*),sum(u_act) from g1 join users using(g_id)
group by g_name

do the job?

/Ulrich
Result can be obtained by:

SELECT g1.g_name,
(select count(*) from users u1 where g1.g_id = u1.g_id) as users_count,
(select count(*) from users u2 where g1.g_id = u2.g_id and u_act = 1) as
Active_users_count
FROM groups g1

Regards,
Igor
"Igor Kryltsov" <kr******@yahoo.com> wrote in message
news:cf***********@news.hub.org...
Hi,
I am using slightly modified example posted by Doug Younger and answered


by
Tom Lane :)
(http://archives.postgresql.org/pgsql...8/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 | 34
Group_B | 75 | 75
Group_C | 25 | 0 <-- all users are inactive here
Group_D | 0 | 0 <---- Assume that this is a result of UNION
which will add groups without employees

Thank you,

Igor



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: RCS | last post by:
I've been running into more and more complexity with an application, because as time goes on - we need more and more high-level, rolled-up information. And so I've created views, and views that use...
7
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
0
by: Michael Manuel via .NET 247 | last post by:
I seem to be having a lot of trouble using the CheckBoxList Control. I have created two pages to try and find out what I have been doing wrong. Both pages are causing me problems but different...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
28
by: Jay | last post by:
OK, I have this XML doc, i dont know much about XML, but what i want to do is take certain parts of the XML doc, such as </title> blah </title> and take just that and put onto a text doc. Then...
1
by: EnjoyNews | last post by:
I have some tables that is connected in a table like this: TracklistID CDid SongID it could look like this 1 1 1
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
7
by: pbd22 | last post by:
Hi. I really need some advice on fine-tuning a stored procedure that is the meat of the search logic on my site. Customers are allowed to save searches, which dumps the search logic in a table...
1
by: alexwm20 | last post by:
I have sqldatasources that are used to display information in a gridview. The data that is being collected comes from 3 different tables. From the one table called comments all i want is a count of...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.