我有这个查询
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,因此具有相同的等级。如何从输出中完全忽略具有相同等级的这些记录?
谢谢!
答案 0 :(得分: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是不同的。但是,你似乎仍然希望消除两者。