Sql 在配置单元的子组中使用秩

Sql 在配置单元的子组中使用秩,sql,hadoop,hive,Sql,Hadoop,Hive,嘿,我正在尝试使用rank让这个查询工作,但我没有运气 select t.orig, t.id, count(*) as num_actions, rank() over (partition by t.orig order by count(*) desc) as rank from sample_table t where rank < 21 and t.month in (201607,20608,201609,201610,201611,201612) and t.orig

嘿,我正在尝试使用rank让这个查询工作,但我没有运气

select t.orig, t.id, count(*) as num_actions, 
rank() over (partition by t.orig order by count(*) desc) as rank
from sample_table t
where rank < 21 
and t.month in (201607,20608,201609,201610,201611,201612) 
and t.orig in (select tw.pageid from tw_sample as tw limit 50) 
group by t.orig, t.id
我一直在

失败:SemanticException[错误10004]:行4:6无效的表别名或列引用“rank”

我的目标是根据count*参数获取每个t.orig的前20行


如果您也能解释一下我的错误所在,以便我能从中吸取教训,那将不胜感激。

您不能在where子句中使用别名。使用子查询:

select *
from (select t.orig, t.id, count(*) as num_actions, 
             rank() over (partition by t.orig order by count(*) desc) as rnk
      from sample_table t
      where t.month in (201607, 20608, 201609, 201610, 201611, 201612)  and
            t.orig in (select tw.pageid from tw_sample tw limit 50) 
      group by t.orig, t.id
     ) t
where rank < 21