“必须聚合相关标量子查询”尽管聚合了相关标量子查询仍然出错? - 火花SQL
我有一个类似于以下内容的查询:
SELECT (SELECT first(b.id) FROM table_b b WHERE b.person = a.student LIMIT 1) student_id,
(SELECT first(b.id) FROM table_b b WHERE b.person = a.teacher LIMIT 1) teacher_id,
'additional_field' AS additional_field
FROM table_a a
这会产生以下错误:
The SQL expression for node [ SQLNode7 ] is invalid. Reason: [ Correlated scalar subqueries must be aggregated: GlobalLimit 1
请注意,此查询在 Redshift 中运行得非常好(没有 first()
)
最初,我没有添加 first()
聚合,但我在收到此错误后这样做了。但是添加后这个错误仍然存在。
我尝试过的其他一些事情:
- 使用
max()
而不是first()
: - 尝试
max(first())
时出现同样的错误:错误说明原因:[不允许在另一个聚合函数的参数中使用聚合函数。请在子查询中使用内部聚合函数。
我是否没有正确聚合此查询?我还可以尝试根据此错误“聚合”我的查询吗?
最小可重现示例:
输入表:
table_a
student teacher
A Z
B Z
C Z
table_b
id person
1 A
2 B
3 C
4 Z
输出:
table_c
student_id teacher_id
1 4
2 4
3 4
I have a query that resembles:
SELECT (SELECT first(b.id) FROM table_b b WHERE b.person = a.student LIMIT 1) student_id,
(SELECT first(b.id) FROM table_b b WHERE b.person = a.teacher LIMIT 1) teacher_id,
'additional_field' AS additional_field
FROM table_a a
This yields the following error:
The SQL expression for node [ SQLNode7 ] is invalid. Reason: [ Correlated scalar subqueries must be aggregated: GlobalLimit 1
Note that this query works perfectly fine in Redshift (without the first()
)
Initially, I didn't add the first()
aggregation, but I did so after getting this error. However, this error still persists even after adding it.
Some other things I've tried:
- using
max()
instead offirst()
: same error - trying
max(first())
: error statingReason: [ It is not allowed to use an aggregate function in the argument of another aggregate function. Please use the inner aggregate function in a sub-query.
Have I not aggregated this query correctly? Is there anything else I can try to "aggregate" my query as per this error?
Min Reproducible Example:
Input Tables:
table_a
student teacher
A Z
B Z
C Z
table_b
id person
1 A
2 B
3 C
4 Z
Output:
table_c
student_id teacher_id
1 4
2 4
3 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不理解
LIMIT 1
的用法:删除它并将子查询包装在聚合函数中 -MIN()
看起来是一个不错的选择 - 添加必需的 <代码>分组依据It doesn’t understand the use of
LIMIT 1
: delete it and wrap your sub queries in an aggregate function -MIN()
looks like a good choice - adding the obligatoryGROUP BY