2017-04-13 65 views
0

我加入兩個表聯繫人和分區,並且該聯合會爲每個城市返回多個記錄。我想選擇每個城市使用排名的任何記錄。以下是我的查詢,但它不起作用。我如何糾正我的查詢?使用排名篩選記錄

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

什麼是 「不工作」 是什麼意思?你能顯示一些輸入數據,實際輸出和預期輸出嗎? – halfer

回答

0

where子句排名前失蹤= 1

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;