【问题标题】:A column aggregation function that determines if all of the values are equal to a constant in Sybase IQ确定所有值是否都等于 Sybase IQ 中的常数的列聚合函数
【发布时间】:2013-11-23 10:08:07
【问题描述】:

我正在尝试编写一个函数来聚合一个非常大的 Sybase IQ 表中的一些文本数据列。我无法更改所提供数据的格式。

每一行代表一个进程的值,有点像一个测试。键是非唯一的,值也是文本字符串,只有“通过”或“失败”。

在实际表中可能有多个“值”列 - 但为简洁起见,我只显示了一个。

规则是,如果所有内容都通过了一个键,那么它就是一个通过。否则就是失败。在一个理想的世界中,我希望能够编写一个有点像的聚合函数:

count(all(mytable.value == 'pass'))

以下是数据示例:

| Key | Value | 
| A   | fail  |
| A   | pass  |
| B   | pass  |
| B   | pass  |
| B   | pass  |
| C   | fail  |
| C   | fail  |

聚合数据如下所示:

| Key | Value |
| A   | fail  |
| B   | pass  |
| C   | fail  |

那么有没有一种优雅的方法来做到这一点?

仅供参考,Sybase IQ - 不是普通的 Sybase! ;-)

【问题讨论】:

  • 不确定系统的具体功能 - 但如果您能够计算 MIN(Value)MAX(Value) 并且它们都返回相同的结果,那么所有单独的行都共享相同的价值也。

标签: sql sybase sap-iq


【解决方案1】:

我不知道 Sybase-IQ,但使用标准 SQL 你可以这样做:

select key, min(value) as value
from mytable
group by key
having sum(case when value = 'pass' then 1 else 0 end) = count(value)
    or sum(case when value = 'fail' then 1 else 0 end) = count(value);

或者使用 damien 的建议:

select key, min(value)
from mytable
group by key
having min(value) = max(value)

【讨论】:

    【解决方案2】:

    这似乎是最简单的解决方案...

    select
      key
      ,sum(case when value = 'pass' then 1 else 0 end) as num_passed
      ,count(*) as num_tests
    from mytable
    group by key
    having num_tests = num_passed
    

    【讨论】:

    • 导致:| Key | Value |/n | B | pass |。失败者去哪儿了?这是错误的。
    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 2018-09-27
    • 2018-10-12
    • 2021-02-27
    相关资源
    最近更新 更多