Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

398
Views
group by year and boolean column postgres

I have the following table

id   |  created_on   | is_a   | is_b    | is_c
----------------------------------------------
1    |  01-02-1999   | True   |False    |False
2    |  23-05-1999   | False  |True     |False
3    |  25-08-2000   | False  |True     |False
4    |  30-07-2000   | False  |False    |True
5    |  05-09-2001   | False  |False    |True
6    |  05-09-2001   | False  |True     |False
7    |  05-09-2001   | True   |False    |False
8    |  05-09-2001   | True   |False    |False

In the table resulting the query, I would like to group by year of creation, and then be able to compare how many records were created in each year for is_a and is_b. I want to completely ignore from the count is_c.

count_a | count_b  | by_creation_year
-----------------------------------------------
1       |1         | 1999
0       |1         | 2000
2       |1         | 2001

I tried the following query:

select count(is_a = True) a, 
       count(is_b = True) b,
       date_trunc('year', created_on)
from cp_all
where is_c = False  -- this removes the records where is_c is True
group by date_trunc('year', created_on)
order by date_trunc('year', created_on) asc;

But I get a table where the count of a and b is exactly the same.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Your count() argument evaluates to true or false which each gets counted as 1 regardless.

You want to use filter

select count(*) filter (where is_a) a, 
       count(*) filter (where is_b) b,
       date_trunc('year', created_on)
  from cp_all
 where is_c = False  -- this removes the records where is_c is True
 group by date_trunc('year', created_on)
 order by date_trunc('year', created_on) asc;

Doing it this way you will not need the where clause.

over 4 years ago · Santiago Trujillo Report

0

That's because count does not take boolean expressions it simply uses the expression and evaluates to check whether it is null or not null to add to the counter. So in this case you should use sum with case

select sum(case when is_a then 1 else 0 end) a, 
       sum(case when is_b then 1 else 0 end) b,
       date_trunc('year', created_on)
from cp_all
where is_c = False  -- this removes the records where is_c is True
group by date_trunc('year', created_on)
order by date_trunc('year', created_on) asc;
over 4 years ago · Santiago Trujillo Report

0

Although I like filter, this is simpler to type:

select sum(is_a::int) as a, 
       sum(is_b::int) as b,
       date_trunc('year', created_on)
from cp_all
where is_c = False  -- this removes the records where is_c is True
group by date_trunc('year', created_on)
order by date_trunc('year', created_on) asc;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!