Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6076307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:35:33+00:00 2026-05-23T10:35:33+00:00

I have a table with users. Each user has a country. What I want

  • 0

I have a table with users. Each user has a country. What I want is to get the list of all countries with the numbers of users and the percent/total. What I have so far is:

SELECT
country_id,
COUNT(*) AS total,
((COUNT(*) * 100) / (SELECT COUNT(*) FROM users WHERE cond1 = true AND cond2 = true AND cond3 = true)::decimal) AS percent
FROM users
WHERE cond1 = true AND cond2 = true AND cond3 = true
GROUP BY contry_id

Conditions in both of queries are the same. I tried to do this without a subquery but then I can’t get the total number of users but total per country. Is there a way to do this without a subquery? I’m using PostgreSQL. Any help is highly appreciated.
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:35:34+00:00Added an answer on May 23, 2026 at 10:35 am

    I am not a PostgreSQL user but, the general solution would be to use window functions.

    Read up on how to use this at http://developer.postgresql.org/pgdocs/postgres/tutorial-window.html

    Best explanation i could use to describe it is: basically it allows you to do a group by on one field without the group by clause.

    I believe this might do the trick:

    SELECT 
        country_id, 
        COUNT(*) OVER (country_id) 
        ((((COUNT(*) OVER (country_id)) * 100) / COUNT(*) OVER () )::decimal) as percent
    FROM 
        users
    WHERE
        cond1 = true AND cond2 = true AND cond3 = true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table named users and each row (user) has an image. The
I have a USERS table. Each user has connections in a CONNECTIONS table. Each
I have a users table, and each user has a unique user_id . Each
I have a mysql table with my users... each user has various attributes like
I have a table of users and each user has another user that they
Scenario: We have 5 users. (users table) Each user has up to 10 imgs.
I have a table users with primary index userId . Currently, each user has
I already have a table in phpmyadmin that contains users records. Each user has
I have a user table 'users' that has fields like: id first_name last_name ...
I'm building a social networking website. I have a table of Users. Each user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.