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