我尝试在Exasol数据库中使用SQL编写一个子查询。这个问题类似于这个线程(SQL查询-在小于或等于日期时加入(,代码在mysql和postgres中运行良好。然而,当我将代码移到Exasol时,它显示SQL错误42000:on子句中的correlation。我想知道这个问题是否有其他解决方案,或者我如何在Exasol中解决它?
SELECT a.ID,
a.join_date,
a.country,
a.email,
b.start_date,
b.joined_from
FROM a
LEFT JOIN b
ON a.country = b.country
AND b.start_date = (
SELECT MAX(start_date)
FROM b b2
WHERE b2.country = a.country
AND b2.start_date <= a.join_date
);
尽管Exasol不支持关联查询,但可以使用DENSE_RANK((SQL函数来解决该需求,如下所示
with cte as (
select
a.ID, a.join_date, a.country, a.email, b.start_date, b.joined_from,
dense_rank() over (partition by b.country order by b.start_date desc) r1
from a
left join b
on a.country = b.country
)
select * from cte where r1 = 1