使用秩过滤记录

sauutmhj  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(353)

我加入了两个表contact和district,这个连接返回了每个城市的多个记录。我想用排名为每个城市挑选一个记录。下面是我的查询,但它不工作。如何更正我的查询?

  1. Select p.name,
  2. p.city,
  3. p.district,
  4. RANK() over (partition by p.city order by p.district asc) as rank
  5. from (select
  6. d.name,
  7. c.city,
  8. c.district
  9. from contact c inner join district d
  10. ON d.district = c.district
  11. AND d.districtType ='d'
  12. AND d.nametype='2'
  13. AND c.district like 'E%'
  14. where c.city in(1316,1515,19393,8026))p
  15. rank=1;
f8rj6qna

f8rj6qna1#

秩=1之前缺少where子句

  1. Select p.name,
  2. p.city,
  3. p.district,
  4. RANK() over (partition by p.city order by p.district asc) as rank
  5. from (select
  6. d.name,
  7. c.city,
  8. c.district
  9. from contact c inner join district d
  10. ON d.district = c.district
  11. AND d.districtType ='d'
  12. AND d.nametype='2'
  13. AND c.district like 'E%'
  14. where c.city in(1316,1515,19393,8026))p
  15. where rank=1;

相关问题