Sql 如何将子查询结果作为另一个查询的列名返回?
我正在尝试计算配置单元中视图的记录计数的平均/最小/最大值Sql 如何将子查询结果作为另一个查询的列名返回?,sql,hive,Sql,Hive,我正在尝试计算配置单元中视图的记录计数的平均/最小/最大值 select avg(rec_count), max(rec_count), min(rec_count) 并尝试实现如下内容: select avg(rec_count), max(rec_count), min(rec_count) where rec_count in (select count(record_number) as rec_count from archives group by record_number);
select avg(rec_count), max(rec_count), min(rec_count)
并尝试实现如下内容:
select avg(rec_count), max(rec_count), min(rec_count)
where rec_count in
(select count(record_number) as rec_count from archives
group by record_number);
select avg(rec_count), max(rec_count), min(rec_count)
from (select record_number, count(*) as rec_count
from archives
group by record_number
) a
但我收到一条错误信息:
失败:SemanticException行0:-1列引用无效
子查询sq_1定义中的“rec_count”[select中的rec_count]
countrecord_number作为记录组中的记录计数
记录编号]用作第2:18行的sq_1
我想知道我是否可以在不必创建另一个包含原始表的分组/聚合值的表/视图的情况下执行此操作
样本数据:
记录编号3031有4条记录,4050有6条记录,因此我希望从查询中得到的预期结果是:
平均值=5最小值=4最大值=6
我想你想要的是:
select avg(rec_count), max(rec_count), min(rec_count)
where rec_count in
(select count(record_number) as rec_count from archives
group by record_number);
select avg(rec_count), max(rec_count), min(rec_count)
from (select record_number, count(*) as rec_count
from archives
group by record_number
) a
我添加了一个数据样本来澄清我的问题。我认为a是一个打字错误,但没有它就出现了一个错误。“a”在结尾做什么?编辑:啊,没关系,我找到了答案。非常感谢你的帮助!