如何在 Redshift 中 return 布尔值

How to return boolean value in Redshift

我有一个关注者table

id  person  type counted  expected
1      a     A     0        1
2      a     A     1        0
3      a     B     1        0
4      a     B     2        0
5      a     B     3        4
6      b     C     0        0

首先,我想按 type 分组,然后对 countedexpected

求和进行汇总
person type sum(counted)  sum(expected)
a      A      1            1
a      B      6            4
b      C      0            0

然后我想添加布尔值是否sum(counted)等于sum(expected)

person type sum(counted)  sum(expected)  counted=expected
a      A      1            1              true
a      B      6            4              false
b      C      0            0              true

然后我想在 person 和 return 布尔值与 andperson

中分组
person  has_false
a        false
b        true

有什么办法可以实现吗?

我说到一半还没继续

select person,type,sum(counted),sum(expected)
from table
group by person,type

如果有人有意见,请告诉我

谢谢

这应该有效。我已经按照您的描述进行了布局,但我认为您不需要按人、类型进行求和 - 而只是按人求和就可以了(对于这个例子)。

drop table if exists test;
create table test (id  int, person  varchar(1), typ varchar(1), counted  int, expected int);

insert into test values
(1, 'a', 'A', 0, 1),
(2, 'a', 'A', 1, 0),
(3, 'a', 'B', 1, 0),
(4, 'a', 'B', 2, 0),
(5, 'a', 'B', 3, 4),
(6, 'b', 'C', 0, 0);

with grouped as (
select person, typ, sum(counted) as scount, sum(expected) as ecount, scount = ecount as equal
from test
group by person,typ
)
select person, bool_and(equal) as has_false
from grouped
group by person;