无效的操作:在子计划目标列表中找不到变量-RedShift

时间:2018-10-24 22:35:09

标签: postgresql amazon-redshift

我有以下查询

  create table #m3
  ( recordDate date ,
  factor float);
  commit;

    insert into "#m3" (recordDate,factor) 
   values
  (DATE '2018-07-01' ,0.9999),
  (DATE '2018-08-01', 0.9995),
  (DATE '2018-09-01',0.9989),
  (DATE '2018-10-01', 0.9948);

  ---------- Query----- 
 select recordDate ,aggregatedvalue
 from (            
    select
       recordDate ,  sum( factor) AggregatedFactor 
      , sum(AggregatedFactor) over (order by  recordDate asc rows between 2 preceding and current row)   * NULLIF( (count(1) over (order by  recordDate asc rows between 2 preceding and current row))/3,0)  AggregatedValue 
   from  
      #m3
  group by  recordDate 
) t

它给了我以下错误

[Amazon](500310) Invalid operation: variable not found in subplan target list;
1 statement failed.

当我关注以下内容时,它会起作用

---------- Query----- 
 select
       recordDate ,  sum( factor) AggregatedFactor 
      , sum(AggregatedFactor) over (order by  recordDate asc rows between 2 preceding and current row)   * NULLIF( (count(1) over (order by  recordDate asc rows between 2 preceding and current row))/3,0)  AggregatedValue 
   from  
      #m3
  group by  recordDate 

上述查询的结果

recorddate  aggregatedfactor    aggregatedvalue
2018-07-01  1   
2018-08-01  1   
2018-09-01  1   3
2018-10-01  0.99    2.99

当我删除NULLIF时,它也起作用。

---------- Query----- 
select recordDate ,aggregatedvalue
from (             
    select
       recordDate ,  sum( factor) AggregatedFactor 
      , sum(AggregatedFactor) over (order by  recordDate asc rows between 2 preceding and current row)      AggregatedValue 
   from  
      #m3
  group by  recordDate 
) t

上述查询的结果

recorddate  aggregatedvalue
2018-07-01  1
2018-08-01  2
2018-09-01  3
2018-10-01  2.99

NULLIF可能引发异常的任何原因

0 个答案:

没有答案