sql - Join Criteria Priority Issue -
hello stack overflow community,
i new sql, might overlooking simple. reason when add t2 criteria clause, t1. criteria no longer followed. example when run query without specifications on diagnosis, see inpatients between specified admitdatetimes, 18 years , older. when add diagnosis criteria, joined table has patients of status, admitdatetime , age.
any , assistance appreciated
thanks!
select top 1000 reporttraining.dbo.iatricgetage(t1.birthdatetime,t1.servicedatetime) 'age' ,t1.[sourceid] ,t1.[visitid] ,t1.[abstractstatusdatetime] ,t1.[accountnumber] ,t1.[admitdatetime] ,t1.[admitfromer] ,t1.[birthdatetime] ,t1.[dischargedatetime] ,t1.[dischargedispositionid] ,t1.[dischargedispositionname] ,t1.[facilityid] ,t1.[facilityname] ,t1.[locationid] ,t1.[locationname] ,t1.[name] ,t1.[observationdatetime] ,t1.[observationpatient] ,t1.[patientstatusname] ,t1.[ptstatus] ,t1.[reasonforvisit] ,t1.[servicedatetime] ,t1.[status] ,t1.[unitnumber] ,t1.[rowupdatedatetime] ,t1.[abstractid] ,t1.[patientid] ,t2.[visitid] ,t2.[diagnosisseqid] ,t2.[diagnosis] ,t2.[abstractid] [livedb].[dbo].[abstractdata] t1 inner join [livedb].[dbo].[absdrgdiagnoses] t2 on (t2.abstractid = t1.abstractid , t2.visitid = t1.visitid) t1.admitdatetime between '2016-01-01' , '2016-01-31' , t1.patientstatusname 'inpatient' , reporttraining.dbo.iatricgetage(t1.birthdatetime,t1.servicedatetime)>= '18' , t2.diagnosis in ('g62.1' , 'i42.6' , 'k29.2' , 'k70' , 't39' , 't40' , 't41.3' , 't51.91xa' , 'z71.41' , 'z72.51') or t2.diagnosis 'f%' , t2.diagnosis not 'f01%' , t2.diagnosis not 'f02%' , t2.diagnosis not 'f03%' , t2.diagnosis not 'f17%' , t2.diagnosis not 'f70%' , t2.diagnosis not 'f71%' , t2.diagnosis not 'f72%' , t2.diagnosis not 'f73%' , t2.diagnosis not 'f74%' , t2.diagnosis not 'f75%' , t2.diagnosis not 'f76%' , t2.diagnosis not 'f77%' , t2.diagnosis not 'f78%' , t2.diagnosis not 'f79%';
and
evaluated before or
- whenever mix them should add parentheses make sure conditions grouped appropriately. in case need group 2 or
conditions evaluated 1 group:
where ... , ... , ... , ( t2.diagnosis in (...) or t2.diagnosis 'f%' ) , ....
Comments
Post a Comment