2012-12-03 42 views
0

我有這個疑問RANK()OVER忽略等於行列

SELECT 
patientid, 
practiceid, 
visitcount 
    FROM 
    (  
     SELECT 
      patientid, 
      practiceid , 
      visitcount, 
      RANK() OVER (PARTITION BY patientid ORDER BY visitcount DESC) as Rank 
     FROM 
      aco.patients_practices 
    WHERE practiceid in (select id from aco.practices where parentaco = 30982) and isprimary = 0 
    ) AS A 
WHERE 
    Rank = 1 

這裏有一些結果

 
patientid  practiceid  visitcount 
157053   30976   6 
158463   30974   2 
187772   30973   15 
187797   30971   1 
187797   30975   1 

通知最後2個patientid的相同,並且具有相同的visitcount因此排名相同。我怎樣才能從輸出中完全忽略這些記錄?

謝謝!

+1

你的子查詢的結果是什麼?其中包含'rank'。 – Taryn

回答

3

您可以通過計算它們並將它們包括在where子句中來消除它們。下面的查詢計算他們使用類似等級的邏輯 - 的次數,患者具有相同的visitcount:

SELECT patientid, practiceid, visitcount 
FROM (SELECT patientid, practiceid, visitcount, 
      RANK() OVER (PARTITION BY patientid ORDER BY visitcount DESC) as Rank, 
      COUNT(*) over (PARTITION by patientid, visitcount) as RankCount 
     FROM aco.patients_practices 
     WHERE practiceid in (select id from aco.practices where parentaco = 30982) and isprimary = 0 
    ) A 
WHERE Rank = 1 and RankCount = 1 

我注意到practiceid是在過去的兩個記錄不同。不過,你似乎仍然想要消除這兩種情況。

+0

這就是門票!謝謝!是的,即使練習不同,我仍然希望他們離開。 – dbinott