2017-05-09 104 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明確檢查布爾值時,我總能修復它。