此查询在 Athena 外部(在 DB2 中)运行。在大多数情况下,当我注释掉“having max(column1) > 3”行时,它在 Athena 中工作。但是,当我“RUN”时,Athena 返回的错误是 SYNTAX_ERROR: line 23:9: 不支持给定的相关子查询。该行对应于“并且不存在”:
select
*
from t1
where
product = 'SHIRT'
and t1.year = '2022' and t1.month = '07' and t1.day ='31'
and not exists
(
select max(column1) from a2
where trim(a2.id) = trim(t1.id) --these are strings with trailing spaces, hence the trim here
having max(column1) > 3
)
limit 10
Athena 似乎对having 子句很满意,因为我写了这样的声明并且它起作用了:
select max(column1) from a2 having max(column1) > 3
当我将它放在“并且不存在”中时,它似乎会抛出一些东西?不知道是不是这个原因...
这不是“并且不存在”的声明,因为我以前使用过它并且它有效。
我不明白雅典娜在烦恼什么。寻找其他想法或如何更好地调试问题?
这可能是一种复杂的实现方式,但我认为这符合您正在寻找的内容。由于查询引擎无法处理某些相关子查询(可能出于性能原因),因此您需要重构代码以不包含该语句。
您需要访问两个表中的 id 才能执行过滤器,因此您需要连接这些表。我认为这一系列的表格表达式应该为您指明正确的方向。从技术上讲,您也可以在子查询中完成所有操作,但不要尝试从子查询内部访问 t1,只需连接到它,然后而不是使用 contains 子句将子查询连接到主查询,使用附加的 where 子句删除您试图排除的行。
with cte1 as (
select *
from t1
where product = 'SHIRT'
and t1.year = '2022'
and t1.month = '07'
and t1.day ='31'
),
cte2 as (
select
a2.id,
max(column1) as abc
from a2
inner join cte1
on trim(cte1.id) = trim(a2.id)
group by a2.id
)
select cte1.*
from cte1
left join cte2
on cte2.abc > 3
and cte2.id = cte1.id
where cte2.id is null
或
select t1.*
from t1
left join (
select
a2.id
max(column1) as abc
from a2
inner join t1
on a2.id =t1.id
) c
on c.id = t1.id
where product = 'SHIRT'
and t1.year = '2022'
and t1.month = '07'
and t1.day ='31'
and c.abc < 3
这里,问题是由 LIMIT 子句引起的。