[英]How to fix “Expressions referencing the outer query…” error in Spark-SQL?
我有一个SQL查询,并在Spark上运行了一个子查询。 我收到此错误:“在WHERE/HAVING clauses
之外不支持引用外部查询的表达式”。 您能帮我找出原因吗?
select distinct NAME from table1, table2 t
where t.ID = (select min(t.ID) from table1 a where a.WID = table1.WID) and
t.WID = table1.WID and
t.VID = table1.VID
错误消息如下:
“ org.apache.spark.sql.AnalysisException:在WHERE / HAVING子句之外不支持引用外部查询的表达式:聚合[min(outer(FAILURE_ID#3104))AS min(outer())#3404]”
学习使用正确的,显式的, 标准的 JOIN
语法!
您可以在FROM
子句中使用所有表引用编写查询:
select distinct NAME
from table1 t1 join
table2 t2
on t2.WID = t1.WID and
t2.VID = t1.VID join
(select tt1.WID, min(tt1.id) as min_id
from table1 tt1
group by tt1.WID
) tt1
on tt1.WID = t1.WID and tt1.min_id = t1.id;
或使用窗口功能:
select distinct NAME
from table2 t2 join
(select t1.*,
min(t1.id) over (partition by t1.WID) as min_id
from table1 t1
) t1
on t2.WID = t1.WID and
t2.VID = t1.VID and
t1.min_id = t1.id;
编辑:
以上假设对您的查询有合理的解释。 要模仿所写的逻辑,可以执行以下操作:
select distinct NAME
from table1 t1 join
table2 t2
on t2.WID = t1.WID and
t2.VID = t1.VID
where t1.ID is not null;
这就是子查询正在做的所有事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.