使用排名过滤记录

时间:2017-04-13 10:00:10

标签: hive bigdata

我正在加入两个表联系人和区域,并且联接正在为每个城市返回多个记录。我想用排名挑选每个城市的任何记录。以下是我的查询,但它无法正常工作。如何更正我的查询?

    Select p.name,
    p.city,
    p.district,
    RANK() over (partition by p.city order by p.district asc) as rank 
    from (select
         d.name,
         c.city,
         c.district 
         from contact c inner join district d 
         ON d.district            = c.district
         AND d.districtType          ='d'
         AND d.nametype='2' 
         AND c.district like 'E%'
         where c.city in(1316,1515,19393,8026))p
    rank=1;

1 个答案:

答案 0 :(得分:0)

在rank = 1之前缺少where子句

 Select p.name,
    p.city,
    p.district,
    RANK() over (partition by p.city order by p.district asc) as rank 
    from (select
         d.name,
         c.city,
         c.district 
         from contact c inner join district d 
         ON d.district            = c.district
         AND d.districtType          ='d'
         AND d.nametype='2' 
         AND c.district like 'E%'
         where c.city in(1316,1515,19393,8026))p
    where rank=1;