【问题标题】:SQL (Redshift) error when using case when - this type of correlated subquery pattern is not supported使用 case when 时出现 SQL (Redshift) 错误 - 不支持这种类型的相关子查询模式
【发布时间】:2020-01-17 15:23:54
【问题描述】:

我正在尝试使用以下内容返回几个“平均 if”列:

select 
    date,
    avg(case when hour >= 23 or hour <= 6) then (select price) else null end) as price1,
    avg(case when (hour >= 16 and hour <= 18) then (select price) else null end) as price2
from 
    xxxxxxxxx
where 
    date <= '2019-12-31' and
    date >= '2018-12-01'
group by 
    date
order by  
    date

当我单独使用每个 avg(case when) 时它可以工作,但是当我同时使用它们时会出现错误

无效操作:由于内部错误,不支持这种类型的关联子查询模式

【问题讨论】:

  • 欢迎使用 StackOverflow!你确定你在第 2 行的括号是正确的?
  • 只需将(select price) 替换为price

标签: sql amazon-redshift


【解决方案1】:

为什么select 中有select

select date,
       avg(case when hour >= 23 or hour <= 6 then price end) as price1,
       avg(case when hour >= 16 and hour <= 18 then price end) as price2
from xxxxxxxxx
where date <= '2019-12-31' and
      date >= '2018-12-01'
group by date
order by date;

else null 也是多余的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多