Sql 配置单元表达式不在按键分组中(按键分组)

Sql 配置单元表达式不在按键分组中(按键分组),sql,hive,hiveql,top-n,Sql,Hive,Hiveql,Top N,我需要执行此查询来创建每个州和行业的平均增长率表 create table industry_growth as select state,sub_industry, avg(growth_percent)ind_growth from company_growth group by state, sub_industry having count(sub_industry>2); 我在这张桌子上看书 state string

我需要执行此查询来创建每个州和行业的平均增长率表

create table industry_growth as select state,sub_industry, avg(growth_percent)ind_growth from company_growth group by state, sub_industry having count(sub_industry>2);
我在这张桌子上看书

state                   string                                      
sub_industry            string                                      
companyname             string                                      
growth_percent          double     
但我总是犯这个错误

失败:SemanticException[错误10025]:第1:45行表达式不在 按关键“子行业”分组


救命啊

如果每个州需要排名前n的子行业,请计算密集度排名,并过滤每个州需要多少顶级行业。此查询将打印每个州的前2个子行业。如果每个州只需要一个顶级子行业,请将where子句中的过滤器更改为where rnk=1。如果两个行业具有相同的工业增长率,它们将获得相同的排名,你将得到两行而不是一行。行编号而不是密集列将仅为一条记录分配1

create table industry_growth as 

select state, sub_industry, ind_growth
from
(
select state, sub_industry, ind_growth , 
       dense_rank() over(partition by state, order by ind_growth desc) rnk
from
  (
   select state, sub_industry, avg(growth_percent) ind_growth 
     from company_growth group by state, sub_industry
  ) s 
) s where rnk <= 2 --for two most performing sub-industries per state
                   --rnk=1 is the most performing sub-industry per state

如果sub_industry在组中,则这不起作用:countsub_industry>2。你想干什么?请解释一下,我正在努力让每个州都有一个发展最好的产业,我认为问题出在括号里。您是否可以尝试创建以下表格:选择州、子行业、按州划分的公司增长组中的平均增长百分比、子行业的数量子行业>2;-请注意,我把>2放在计数之外。但我不确定您的逻辑。请提供一些有代表性的数据示例和预期结果countsub_industry>2应该做什么?你需要两个增长最好的压水堆状态吗?或者举个例子?