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

  • Home
  • SEARCH
  • 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 7440463
In Process

The Archive Base Latest Questions

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

I am running PostgreSQL. I have a table called foo .Its content are: city|number

  • 0

I am running PostgreSQL. I have a table called foo.Its content are:

city|number
'oo'|12
'ss'|11
'ss'|23
'oo'|11
'pp'|21

If I run a query like select count(city) from foo group by city having number<21 I will get

city|number
'oo'|2
'ss'|2

but I want the result to consider all the possible cases of city like this:

city|number
'oo'|2
'ss'|2
'pp'|0

How should the query look like?

  • 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-29T10:51:23+00:00Added an answer on May 29, 2026 at 10:51 am

    This is could be solved with a left join:

    select f1.city, count(f2.number) as total from foo as f1
    left join foo as f2
    on f1.cityId = f2.cityId and f1.number < 21
    group by f1.city
    

    Here is a working example

    PS: Your example is wrong because ss should have a value of 1, not 2:

    YOUR EXAMPLE         WHAT I THINK YOU REALLY WANT
    city|number                   city|number
    'oo'|2                        'oo'|2
    'ss'|2                        'ss'|1
    'pp'|0                        'pp'|0 
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
I'm running a query against a table in a postgresql database. The database is
I have a really big database (running on PostgreSQL) containing a lot of tables
I have a Rails 2.0.2 application running with a postgresql db. The machine will
I have a table that I would like to be able to present ranked
I'm running a Postgresql query and getting the following error: ActiveRecord::StatementInvalid (PGError: ERROR: could
I have postgresql running (/opt/local/lib/postgresql90/bin). The data base is set up @ /Users/demet8/postgres/data. I
I have the latest 64 bit version of PostgreSQL. I am running Win 7
I have a table in PostgreSQL that I need read into memory. It is
I have a table in my postgresql db holding a state of an hour

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.