0

我有这个查询

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 就诊次数
157053 30976 6
158463 30974 2
187772 30973 15
187797 30971 1
187797 30975 1

请注意,最后 2 个患者 ID 是相同的,并且访问次数相同,因此排名相同。如何从输出中完全省略这些具有相同等级的记录?

谢谢!

4

1 回答 1

3

您可以通过计算它们并将它们包含在where子句中来消除它们。以下查询使用类似于排名的逻辑对它们进行计数——患者具有相同就诊次数的次数:

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 不同。不过,您似乎仍然想消除两者。

于 2012-12-03T22:05:31.533 回答