Sql 按年份分组和布尔列postgres

Sql 按年份分组和布尔列postgres,sql,postgresql,date,group-by,Sql,Postgresql,Date,Group By,我有下表 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

我有下表

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
在查询结果表中,我希望按创建年份分组,然后能够比较
is_a
is_b
每年创建的记录数。我想完全忽略计数
is\u c

count_a | count_b  | by_creation_year
-----------------------------------------------
1       |1         | 1999
0       |1         | 2000
2       |1         | 2001
我尝试了以下查询:

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;

但是我得到一个表,其中a和b的计数完全相同。

这是因为
count
不采用布尔表达式,它只是使用表达式并计算以检查它是否为
null
notnull
添加到计数器中。因此,在这种情况下,您应该将
sum
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;

这是因为
count
不接受布尔表达式,它只是使用表达式并计算以检查它是
null
还是
notnull
添加到计数器中。因此,在这种情况下,您应该将
sum
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;
您的
count()
参数的计算结果为
true
false
,每个参数都被计为
1

您想使用
过滤器

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;
通过这种方式,您将不需要
where
子句。

您的
count()
参数的计算结果为
true
false
,这两个参数都被计为
1

您想使用
过滤器

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;

通过这种方式,您将不需要
where
子句。

虽然我喜欢过滤器,但它更易于键入:

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;

虽然我喜欢filter,但它的类型更简单:

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;

你很接近。只需在您的选择中将
count
更改为
sum
,因为您的条件不会返回聚合中会忽略的
null
。相反,它返回2
非空值--0和1非常接近。只需在您的选择中将
count
更改为
sum
,因为您的条件不会返回聚合中会忽略的
null
。相反,它返回2个
非空值--0和1