我很高兴这次我至少有一个工作示例的问题。这曾经是一个有效的查询,当我只有一个条件时,如果结果的计数 >= 1 则返回结果。然后我必须对不同的代码值进行额外的计数,以及它们是否出现 2 次或更多次。查询从几秒钟内运行到大约 43 秒。
我认为我的逻辑是正确的,但我想知道是否有人有更有效的方法来做到这一点。
select person.person_id
from person
where
person.person_id in (
select procedure.person_id
from procedure
where
procedure.performed_by = '555555'
and procedure.code in (
'99201', '99202'
)
and year(procedure.service_date) = year(getdate())
group by procedure.person_id
having count(1) >= '1'
) -- having count >= 1 occurrences
or person.person_id in (
select person_id
from procedure
where
procedure.performed_by = '55555'
and code in (
'99304','99305'
)
and year(procedure.service_date) = year(getdate())
group by procedure.person_id
having count(1) >= '2'
) -- having count >= 2 occurrences