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 6147013
In Process

The Archive Base Latest Questions

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

shelter_inventory (table) gid | pav_type | yes 1 | 1 | 1 2 |

  • 0

shelter_inventory (table)

gid | pav_type | yes
 1  |  1       | 1
 2  |  1       | 1
 3  |  0       | 1
 4  |  2       | 1 
 5  |  2       | 0

this is the current query (does not display count = 0)

SELECT pav_type, count(*) FROM shelter_inventory 
WHERE yes = 1 GROUP BY pav_type ORDER BY pav_type

and I want the result to display like this

pav_type | count(*)
 0       |  1
 1       |  2
 2       |  1 
 3       |  0

How could I query on this case? I use PostgreSQL.

  • 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-23T19:02:34+00:00Added an answer on May 23, 2026 at 7:02 pm

    If your pav_type values are sequential, then you could use generate_series in place of an external table:

    select p.pav_type, count(yes)
    from generate_series(0,3) as p(pav_type)
    left outer join shelter_inventory s
      on p.pav_type = s.pav_type and s.yes = 1
    group by p.pav_type
    order by p.pav_type
    

    This yields:

     pav_type | count 
    ----------+-------
            0 |     1
            1 |     2
            2 |     1
            3 |     0
    

    This:

    generate_series(0,3) as p(pav_type)
    

    essentially generates an inlined table with a single column called pav_type and four rows: 0, 1, 2, 3. And you need to have the s.yes = 1 in the join condition (rather than the WHERE) because you want the yes = 0 values to be in the pre-grouped result set; if s.yes = 1 is in the WHERE clause then the yes = 0 rows won’t be counted regardless of what the join condition is.

    If your pav_types do not nicely fit with generate_series (i.e. not sequential or step-sequential) and you only have a small number of them, you could join to a VALUES expression:

    select p.pav_type, count(yes)
    from (values (0), (1), (2), (3)) as p(pav_type)
    left outer join shelter_inventory s
      on p.pav_type = s.pav_type and s.yes = 1
    group by p.pav_type
    order by p.pav_type
    

    You need to make sure you get all the parentheses in the right places of course.

    If you do have the pav_types in a separate table then do a LEFT OUTER JOIN to that table instead of using the generate_series. If your pav_types are not sequential and you have too many to sensibly put in a VALUES expression, then build a table to hold the valid pav_type values and LEFT OUTER JOIN to that.

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

Sidebar

Related Questions

I have a Linq-to-Entities query that is not complicated but requires an .include and/or
a collegue of mine proposed to me an exercise from an online judge website,
I am coding a little script to save some data from Internet every single
I have only made a 100% of the whole page, but this time I
I want to fetch 10.2.0.4.0 from tnsping output in AIX, so I wrote tnsping
I have a database table with categories for different products in it. Each category
I have a view which accepts the following model: Inherits=System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel> This posts back to
I'd like to add different types of objects derived from one class with generics
What's the strategy for removing descendant records from a given record ID where the
I have a csv file like, let's say table.csv foo;3 bar;1 and a script

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.