2017-04-25 28 views
1
子組內使用排名

嘿,我試圖讓這個查詢使用rank()工作,但我有沒有運氣,在HIVE

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無效的表別名或列參考'級'

我的目標是抓住每個t.orig前20行的基礎上count(*)參數。

如果你也能解釋我哪裏錯了,所以我可以從中學到,那將不勝感激。

回答

1

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 
+0

完美,謝謝! –