SQL Server : if one value in a column occurs at least once or another value more than once -
i'm glad have question @ least working example time. used efficient query when had criteria result returned if had count >= 1. had additional count different code values , if occur 2 or more times. query went running in few seconds 43 seconds.
i think have logic right wondering if had more efficient way this.
select person.person_id person person.person_id in ( select procedure.person_id procedure procedure.performed_by = '555555' , procedure.code in ( '99201', '99202' ) , year(procedure.service_date) = year(getdate()) group procedure.person_id having count(1) >= '1' ) -- having count >= 1 occurrences or person.person_id in ( select person_id procedure procedure.performed_by = '55555' , code in ( '99304','99305' ) , year(procedure.service_date) = year(getdate()) group procedure.person_id having count(1) >= '2' ) -- having count >= 2 occurrences
does speed up?
with cte ( select procedure.person_id procedure procedure.performed_by = '555555' , procedure.code in ( '99201', '99202' ) , year(procedure.service_date) = year(getdate()) group procedure.person_id having count(1) >= '1' union select person_id procedure procedure.performed_by = '55555' , code in ('99304','99305') , year(procedure.service_date) = year(getdate()) group procedure.person_id having count(1) >= '2' ) select person.person_id person join cte on cte.person_id = person.person_id
Comments
Post a Comment