sqlsubqueryamazon-redshiftcorrelated-subquerysql-optimization

Correlated subquery pattern is not supported due to internal error - where not exists correlated subquery


I have a query that is giving me the error above. My code is the following:

SELECT *,
      dense_rank() OVER (PARTITION BY email
                         ORDER BY priority_score,
                                  comp) AS r
FROM main_query
WHERE NOT EXISTS
   (SELECT name,
           event,
           email,
           report_date
    FROM gm
    WHERE  gm.name= main_query.name
      AND gm.event= main_query.event
      AND gm.email = main_query.email
      AND gm.report_date >= (CURRENT_DATE - 25)::date)
ORDER BY priority_score ASC

One solution that I saw to overcome these types of errors was to be able to transform correlated subqueries in queries not correlated (sherlock). Therefore, I am searching for other ways of using the where not exists statement but without a correlated subquery, i.e., calling the main_query table inside the subquery ((...) from gm left join main_query on(...)). Does anyone know if this is possible and how to do it?

Any advice is more than welcome and thanks a lot in advance!


Solution

  • Does a LEFT JOIN version work?

    SELECT mq.*,
          dense_rank() OVER (PARTITION BY mq.email
                             ORDER BY mq.priority_score, comp
                            ) AS r
    FROM main_query mq LEFT JOIN
         gm
         ON gm.name = mq.name AND
            gm.event= mq.event AND
            gm.email = mq.email AND
            gm.report_date >= (CURRENT_DATE - 25)::date)
    WHERE gm.name IS NULL  
    ORDER BY priority_score ASC;
    

    If that doesn't work, it should work like this:

    SELECT mq.*,
          dense_rank() OVER (PARTITION BY mq.email
                             ORDER BY mq.priority_score, comp
                            ) AS r
    FROM main_query mq LEFT JOIN
         (SELECT gm.*
          FROM gm
          WHERE gm.report_date >= (CURRENT_DATE - 25)::date)
         ) gm
         ON gm.name = mq.name AND
            gm.event= mq.event AND
            gm.email = mq.email
    WHERE gm.name IS NULL  
    ORDER BY priority_score ASC