gpt4 book ai didi

sql - Hive SQL 中的嵌套查询

转载 作者:行者123 更新时间:2023-12-02 05:37:28 24 4
gpt4 key购买 nike

我有一个数据库,我使用查询生成一个像这样的中间表:

id    a      b    
xx 1 2
yy 7 11

我想计算具有 < avg(a) 的用户的 b 标准差

我用这种方式计算 avg(a) 并且效果很好:

select avg(select a from (query to produce intermediate table)) from table;

但是查询:

select stddev_pop(b) 
from (query to produce intermediate table)
where a < (select avg(select a
from (query to produce intermediate table))
from table);

返回一个错误,更准确地说,我被告知 avg(select a from...) 中的“a”未被识别。这让我真的很困惑,因为它在前面的查询中有效。

如果有人能提供帮助,我将不胜感激。

编辑:

我将生成中间表的查询结果存储到临时表中,但仍然遇到同样的问题。非工作查询变为:

select stddev_pop(b) from temp where a < (select avg(a) from temp);

虽然这有效:

select avg(a) from temp;

最佳答案

好的,一位同事帮我做到了。如果有人遇到同样的问题,我会发布答案:

select stddev_pop(b)
from temp x
join (select avg(a) as average from temp) y
where x.a < y.average;

基本上,hive 不会将表缓存为变量。

关于sql - Hive SQL 中的嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13631259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com