嘿,我正在尝试使用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

我不断



我的目标是根据t.orig参数获取每个count(*)的前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

关于sql - 在HIVE子组中使用等级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43620885/

10-12 23:49