Redshift相关子查询内部错误

时间:2017-05-09 02:39:22

标签: sql amazon-redshift

所以我在Amazon Redshift中有一个出价表。每个出价都有说明和出价的用户,以及我想知道的每个出价,如果用户在过去5天内使用相同的说明进行了出价。

查询如下所示:

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。但是,当我在结尾添加“= True”时,它可以工作。

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

这只是一个错误,还是有一些深层原因导致第一个无法完成?

3 个答案:

答案 0 :(得分:1)

我认为编写查询的更好方法是使用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;

答案 1 :(得分:1)

首先尝试运行:

select b1.bid_id
from dim_bid b1
where b1.hidden

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

答案 2 :(得分:1)

文档陈述如下:

  

我们建议始终明确检查布尔值,如下面的示例所示。隐式比较(例如WHERE标志或WHERE NOT标志)可能会返回意外结果。

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

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