嘿,我正在尝试使用 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
我不断得到,
FAILED: SemanticException [Error 10004]: Line 4:6 Invalid table alias or column reference '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
关于sql - 在 HIVE 的子组中使用排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43620885/