【问题标题】:In Amazon Redshift is it possible to have a condition on two columns?在 Amazon Redshift 中是否可以在两列上设置条件?
【发布时间】:2021-09-09 15:18:06
【问题描述】:

我有下表,如下所示。我想为其 Group 提取具有最小 Row_Flag 的行,其中 Record_Flag 等于 0。如果 Record_Flag 不等于 0,逻辑应该提取下一个最小的 Row_Flag,其中 Record_Flag 等于 0。这些行以黄色突出显示。我不确定是否可以在 Redshift 的 SQL 编辑器中执行这种“双重条件”逻辑。是否可以在 Redshift 中计算这种逻辑?如果是这样,我该怎么做?

【问题讨论】:

  • 我删除了 Postgres 标签。如今,Postgres 真的与 RedShift 无关。

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


【解决方案1】:

您可以使用row_number()。我将您的问题解释为您想要最小的row_flag 而record_flag = 0。而record_flag 只取值0 和1。

select t.*
from (select t.*,
             row_number() over (partition by group
                                order by record_flag, row_flag
                               ) as seqnum
      from t
     ) t
where seqnum = 1;

【讨论】:

    猜你喜欢
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2011-08-14
    相关资源
    最近更新 更多