2017-05-09 29 views
0

所以我有一个Amazon Redshift出价表。每个出价都有描述和出价的用户,以及我想知道的每个出价,如果用户在过去5天内使用相同的说明进行了出价。Redshift相关子查询内部错误

查询看起来是这样的:

select b1.bid_id, case when 
    exists(select b2.bid_id from dim_bid b2 WHERE b1.user_id = b2.user_id 
          and b2.bid_timestamp < b1.bid_timestamp and b2.bid_timestamp > b1.bid_timestamp - INTERVAL '5 day' 
          and b2.description = b1.description and b2.bid_timestamp > '2017-04-25') then 'good bid' else 'duplicate bid' END 
    from dim_bid b1 
    where b1.hidden 

不工作,给人的错误:this type of correlated subquery is not supported due to internal error。但是,当我刚刚添加一个“=真”在最后它的作品。

select b1.bid_id, case when 
    exists(select b2.bid_id from dim_bid b2 WHERE b1.user_id = b2.user_id 
          and b2.bid_timestamp < b1.bid_timestamp and b2.bid_timestamp > b1.bid_timestamp - INTERVAL '5 day' 
          and b2.description = b1.description and b2.bid_timestamp > '2017-04-25') then 'good bid' else 'duplicate bid' END 
    from dim_bid b1 
    where b1.hidden = True 

这是一个错误,还是有一些深层的原因,为什么第一个不能做?

回答

0

我认为更好的方式来编写查询使用lag()

select b.*, 
     (case when lag(b.bid_timestamp) over (partition by b.description order by b.timestamp) > b.bid_timestamp - interval '5 day' 
      then 'good bid' else 'duplicate bid' 
     end) 
from dim_bid b; 
0

尝试运行此第一:

select b1.bid_id 
from dim_bid b1 
where b1.hidden 

你会看到,红移将引发不同的错误(例如,WHERE。必须是类型布尔值...)。因此,为了查询运行,必须是布尔值的参数。所以当你添加'= True'时,那么参数是布尔值,并且查询会运行。当查询关联子查询并且查询中存在无效操作时,我注意到红移引发了相关的子查询错误。这可能是由于红移不支持一些相关的子查询(correlated subqueries redshift)。

0

的文档说明如下:

We recommend always checking Boolean values explicitly, as shown in the examples following. Implicit comparisons, such as WHERE flag or WHERE NOT flag might return unexpected results.

参考:http://docs.aws.amazon.com/redshift/latest/dg/r_Boolean_type.html

我不认为这一定是一个错误。我会建议总是检查布尔值为where b1.hidden is True。在使用相关子查询时,我已经看到这个错误很多次了,但是当使用is true/false/unknown明确检查布尔值时,我总能修复它。