【问题标题】:filter (where ...) that works with Postgres but not with Redshift适用于 Postgres 但不适用于 Redshift 的过滤器(其中 ...)
【发布时间】:2020-08-05 17:17:28
【问题描述】:

在 Postgres 中可以使用 Redshift 替代过滤器(在哪里...)? 我收到以下错误,无法确定这在 Redshift 中是不可能的还是我有语法错误: 数据库报语法错误:Amazon Invalid operation: syntax error at or near "(" Position: 1521;

select count(distinct "agent.id")  as "agent_cnt"
, count(distinct "agent.id") filter (where "agent.status" = 'active' and ("agent.date" between '2020-01-01' and current_date)) as "active_agent_cnt"
from "agent" 

【问题讨论】:

  • Redshift 在添加 filter(where) 之前从 PostgreSQL 中分离出来。如果 Redshift 将此功能添加到他们的 fork 中,他们似乎会使用相同的语法,所以它可能不存在。

标签: sql filter amazon-redshift where-clause


【解决方案1】:

使用case 表达式:

count(distinct case when "agent.status" = 'active' and ("agent.date" between '2020-01-01' and current_date then "agent.id" end) as "active_agent_cnt"

在列名中有句点似乎非常可疑。我强烈反对这样的命名约定。

【讨论】:

    【解决方案2】:

    不知道为什么名字中有句号是可疑的。它通过表来识别一列,并且使用 redshift(和 PG)它也有可能也有一个模式可以使用,所以看到schema.table.column 标识符是现实的(但可能不寻常)。

    【讨论】:

    • 这实际上是问题所在 - 不是!引号内的文本是列。所以它不是“table.column”,而实际上是“col.umn”。例如,列“agent.status”位于“agent”表中,因此要识别表和列,您可以编写“agent”.“agent.status”。这可能是原始帖子/问题中的错误/错字(或名称非常容易混淆的列)。
    猜你喜欢
    • 1970-01-01
    • 2021-04-07
    • 2013-04-17
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 2020-02-06
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多