[英]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.