Hive Rank查询问题

时间:2018-06-24 09:50:16

标签: hiveql

select season,violation_code, cnt, 
       RANK() over (Partition BY season order by cnt desc) AS rank 
  from
    (  select season,violation_code, 
              count(*) as cnt 
         from  ParkingViolations_seondary 
     group by season,violation_code
    ) tmp
where rank <= 3

我是Hive的新手。有人可以帮我以上查询有什么问题吗? 它将引发以下错误:

编译语句时出错:

  

失败:SemanticException [错误10004]:第4:6行无效的表别名   或列引用“等级” :(可能的列名称为:季节,   违规代码,cnt)

任何快速帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

使用子查询能够在rank子句中访问where

select season, violation_code, cnt, rnk
from
( select season,violation_code, cnt, 
         RANK() over (Partition BY season order by cnt desc) AS rnk
    from
        (  select season,violation_code, 
                count(*) as cnt 
             from  ParkingViolations_seondary 
         group by season,violation_code
        ) tmp
)s
where rnk <= 3

答案 1 :(得分:0)

是的,我还可以通过以下方法使它工作:

选择*从     (     SELECT season,violation_code,cnt,RANK()over(按季节划分ORDER BY cnt DESC)AS频率     从         (SELECT season,violation_code,COUNT(*)as cnt from ParkingViolations_seondary         其中(违反代码<> 0)和(街道代码1 <> 0或街道代码2 <> 0或街道代码3 <> 0)         按季节分组,违反代码)TMP )TMP1 频率<= 3;