【问题标题】:How to return boolean value in Redshift如何在 Redshift 中返回布尔值
【发布时间】:2022-01-17 17:57:27
【问题描述】:

我有一张下表

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)equalsum(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 中分组,并在person 中返回布尔值and

person  has_false
a        false
b        true

有什么方法可以实现吗?

我去了一半,但还没有继续。

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

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

谢谢

【问题讨论】:

    标签: sql amazon-web-services amazon-redshift


    【解决方案1】:

    这应该可行。我已经按照您的描述进行了布局,但我认为您不需要按人、类型求和 - 只需按人求和即可(对于本示例)。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2015-02-14
      • 2015-04-07
      • 2013-08-18
      相关资源
      最近更新 更多