如何使用groupby在配置单元查询中查找表行计数

k97glaaz  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(335)

我的问题是:我有一个表,其中有一些记录,如(名称、日期、类型)。假设我有三种类型a、b和c。现在我想计算每种类型的百分比平均计数(类型)/计数(表行计数)??

select type,COUNT(type) as counttype,counttype/(select COUNT(*) from xyz) from xyz group by xyz;

“(select count(*)from xyz)”这给了我一个错误。如何查找表行计数?

ccrfmcuu

ccrfmcuu1#

您可以使用以下查询:-

选择a.type,a.type\u cnt,(a.type\u cnt/b.total\u cnt)from(select type,count(type)as type\u cnt from xyz group by type)连接(select count(*)as total\u cnt from xyz)b on 1=1;

swvgeqrz

swvgeqrz2#

使用解析函数,如果不使用join,速度会快得多:

select type,
       count(type) as type_cnt, 
       count(type)/count(*) over() as pct  
  from xyz 
 group by type;

相关问题