简体   繁体   中英

Redshift - SQL Left Join does not work with correlated subquery and aggregated function

I am quite new to Redshift SQL and have been trying to figure out what's wrong with my syntax.

My task is to join 2 tables: question and user via left join as I want to retain all values from table question .

At the moment it throws the following error message: [Amazon](500310) Invalid operation: This type of correlated subquery pattern is not supported yet; when I use left join . On the other hand, when I change the code to join it works just fine. I suspect this is because I have an aggregated function and logical expression within my subquery that makes my left join an inner join.

But as I mentioned above, I need to retain all values from table question .

Below is my code

select
qa.user_id as user_email,
i.timestamp as session_login_time,
qa.timestamp as question_ask_time,
qa.question_id,
qa.question
from 
schema1.question as qa
left join
schema1.user as i
on
    qa.user_id = i.email
and 
    i.timestamp =
        (select
            max(timestamp)
         from schema1.user
         where
            timestamp <= qa.timestamp)
where user_email <> 'tester' and user_email not like '%tester.com'
group by qa.user_id, i.timestamp, qa.timestamp, qa.question_id, qa.question

The purpose of the subquery is to get the closest session_login_time to each of the question_ask_time . So, multiple rows of question can have the same session_login_time value.

Could anybody please help me identify what I am missing from my code above? How do I make my left join works?

Thank you so much!

I guess that should get you the same results without involving a sub query

select
    qa.user_id as user_email,
    max(i.timestamp) as session_login_time,
    qa.timestamp as question_ask_time,
    qa.question_id,
    qa.question
from schema1.question as qa
left join schema1.user as i
on qa.user_id = i.email
and i.timestamp  <= qa.timestamp
where qa.user_id <> 'tester' and qa.user_id not like '%tester.com'
group by qa.user_id, qa.timestamp, qa.question_id, qa.question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM