如何返回子查询结果作为另一个查询的列名?

bq9c1y66  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(299)

我正在尝试计算配置单元中视图的记录计数的平均/最小/最大值

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);

但我收到一条错误信息:
失败:semanticexception行0:-1子查询sq\u 1的定义中的列引用“rec\u count”无效[rec\u count in(select count(record\u number)as rec\u count from archives group by record\u number)]在第2:18行用作sq\u 1
我想知道我是否可以这样做,而不必创建另一个包含原始表的分组/聚合值的表/视图。
样本数据:

记录编号3031有4条记录,4050有6条记录,因此我希望从查询中得到的预期结果是:
平均值=5最小值=4最大值=6

8xiog9wr

8xiog9wr1#

我想你想要的是:

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

相关问题