Count equals 0

From: Rudi Starcevic <rudi(at)oasis(dot)net(dot)au>
To: pgsql-sql(at)postgresql(dot)org
Subject: Count equals 0
Date: 2003-03-18 06:29:13
Message-ID: 3E76BCB9.9070603@oasis.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Greetings,

I have what I think is an easy query but is not working out for me.
So I thought I'd ask ..

I have 2 tables :

user_main
user_sys_messages

In user_main I have many user's which belong to 3 web sites.
In my admin tool I can send a user an email message and that message is
logged to the user_sys_messages table.

All that is good.
What I want to report is those who have *not* recieved any email from me.
If each of the 3 web site's has a user in user_main who has not recieved
any messages the report form the below query
looks like

site a : 1
site b : 3
site c : 6

My problem is if a site has no one that has not recieved any message
they do not appear in the report.
Ie.

site b : 3
site c : 6

What I would like to see is something like:

site a : 0
site b : 3
site c : 6

I've tries Left outer join's with and without coalesce without joy so far.
Please check out my query.

Thank you kindly.

-- new members --- members who haven't recieved any email from us
SELECT um.site_name, count(um.cus_id) AS total
FROM user_main um
WHERE NOT EXISTS
(
SELECT cus_id
FROM user_sys_messages usm
WHERE usm.cus_id = um.cus_id
)
GROUP BY um.site_name
ORDER BY um.site_name

Regards
Rudi.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Frankie Lam 2003-03-18 07:24:23 PLPGSQL with Multibyte Issue
Previous Message Susan Hoddinott 2003-03-18 02:01:16 Re: Create function statement with insert statement