My where exists clause isn’t working. What trivial thing am I missing?
select * from patient as p
where exists
(
select p.patientid, count(*) from tblclaims as c
inner join patient as p on p.patientid=c.patientid
and p.admissiondate = c.admissiondate
and p.dischargedate = c.dischargedate
group by p.patientid
having count(*)>500
)
patient and tblclaims are joined together by the three-field composite key as as you can see in the query.
The
count(*) from tblclaims as cis unnecessary and could be throwing the query off.Also, you have no
WHEREclause joiningpatient pto yourexistsclause.Not to mention you use
pas an alias in both the main query and theexistsclause, which is just confusing.You probably want something like: