Skip to content
Advertisement

Counting organizations which user has not contacted

I am new at PostgreSQL. I have the following tables created in my schema:

User table:

Organization table:

And finally Organization_rating table:

With this schema, an organization has a business type in which gives support to the user that also have that business type. After giving this support, the user can rate this organization with organization_rating. Following this logic, I would like to execute a query which gives me the percentage of the organizations that the user has contacted rated versus the organizations which the user still needs to rate. For example, lets say the user_id 1 rates the organization 1, which have the same business type as the user. 9 more organizations have the same business type, but the user has not rated these organizations. This query would return 10%.

I have the following query to count the organizations which the user has already contacted:

How can I count the organizations which have not been contacted by this user, and the percentage of contacted vs still to be contacted?

If you could also refer me to some sites which can help me learn more of PostgreSQL and the different functions, I would appreciate it. Thank you in advance!

Advertisement

Answer

You can cross join the uses and organizations to get all possible combinations, then bring the bridge table with a left join. The last step is conditional aggregation:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement