我正在尝试重新编写查询,以便它使用标准联接(在 from 子句中)而不是旧版本(迁移到 SQL 服务器,因此被迫进行此转换)。我遇到的问题是我下面的查询版本返回的记录比原始版本少得多。所以我在想我的一个外部联接不是那么外部。感谢您的帮助,特别是如果您确实完成了整个查询。我似乎找不到任何关于如何进行这样的过渡的好文档。
declare
@startdate datetime, @enddate datetime
select @startdate = dateadd(dd,-6,convert(datetime,convert(varchar,getdate(),101)))
select @enddate = dateadd(dd,6,@startdate)
SELECT PAT.CUSID as ID ,
substring(CPTREC.CompletedDateTime,1,12) AS DateOfService,
SUBSTRING(PCMH.ProcedureCode,CHARINDEX('-',PCMH.ProcedureCode)+ 1,50) AS CPT_Code
FROM cpt_records as CPTREC
left Outer Join ProcedureCodeMH as PCMH
on CPTREC.ProcedureCodeRevCount = PCMH.ProcedureCodeRevCount
and CPTREC.ProcedureCodeSer= PCMH.ProcedureCodeSer
left outer join Credit as CR
on CPTREC.ActInstProcCodeSer = CR.ActInstProcCodeSer
and CPTREC.ActInstProcCodeRevCount = CR.ActInstProcCodeRevCount
inner join ActivityInstance as ACTIN
on CPTREC.ActivityInstanceSer= ACTIN.ActivityInstanceSer
and CPTREC.ActivityInstanceRevCount = ACTIN.ActivityInstanceRevCount
inner join ActivityCapture as ACTCAP
on ACTCAP.ActivityInstanceSer= ACTIN.ActivityInstanceSer
and ACTCAP.ActivityInstanceRevCount = ACTIN.ActivityInstanceRevCount
inner join ActCaptDiagnosisMH as ACTCAPDMH
on ACTCAPDMH.ActivityCaptureSer = ACTCAP.ActivityCaptureSer
and ACTCAPDMH.ActivityCaptureRevCount=ACTCAP.ActivityCaptureRevCount
left outer join Department as DEPT
on ACTCAP.DepartmentSer = DEPT.DepartmentSer
left outer join Hospitality as HOSP
on DEPT.HospitalitySer = HOSP.HospitalitySer
Left outer join Diagnosis as DIAG
on ACTCAPDMH.DiagnosisSer = DIAG.DiagnosisSer
inner join TemplateCycle as TEMPCYC
on ACTIN.TemplateCycleSer= TEMPCYC.TemplateCycleSer
inner join Template as PLATE
on TEMPCYC.TemplateSer = PLATE.TemplateSer
inner join Patent as PAT
on PLATE.PatentSer = PAT.PatentSer
Left Outer Join PatentParticular as PATPAR
on PAT.PatentSer = PATPAR.PatentSer
inner join PatentDoctor as PATDOC
on PAT.PatentSer = PATDOC.PatentSer
inner join Doctor as DOC
on PATDOC.ResourceSer = DOC.ResourceSer
WHERE ( CPTREC.CompletedDateTime >= @startdate )
and ( CPTREC.CompletedDateTime < dateadd(dd,1,@enddate) )
and ( CPTREC.ObjectStatus = 'Active' )
and ( PATDOC.OncologistFlag = 0 )
and ( PATDOC.PrimaryFlag = 1 )
and ( PCMH.ProcedureCode like '%77781%'
or PCMH.ProcedureCode like '%77782%'
or PCMH.ProcedureCode like '%77783%'
or PCMH.ProcedureCode like '%77784%'
or PCMH.ProcedureCode like '%77785%'
or PCMH.ProcedureCode like '%77786%'
or PCMH.ProcedureCode like '%77787%')
带回更多记录的原版是
SELECT PAT.CUSID as ID , substring(cpt_records.CompletedDateTime,1,12) AS DateOfService, SUBSTRING(ProcedureCodeMH.ProcedureCode,CHARINDEX('-',ProcedureCodeMH.ProcedureCode)+ 1,50) AS CPT_Code INTO cpt777 FROM cpt_records , ActivityCapture , ActivityInstance , Patent as PAT, ProcedureCodeMH , Template , TemplateCycle , Department , Hospitalityity , Credit , Doctor, PatentDoctor , ActCaptDiagnosisMH , Diagnosis , PatentParticular WHERE ( #cpt_records.ProcedureCodeSer *= ProcedureCodeMH.ProcedureCodeSer) and
( ProcedureCodeMH.ProcedureCodeRevCount =*cpt_records.ProcedureCodeRevCount) and
( ActivityCapture.DepartmentSer *= Department.DepartmentSer) and
( Department.HospitalitySer *= Hospitality.HospitalitySer) and
( ActivityCapture.ActivityCaptureSer *= Credit.ActivityCaptureSer) and
( ActivityCapture.ActivityCaptureRevCount *= Credit.ActivityCaptureRevCount) and
( Credit.ActInstProcCodeSer =* cpt_records.ActInstProcCodeSer) and
( Credit.ActInstProcCodeRevCount =* cpt_records.ActInstProcCodeRevCount) and
( Patent.PatentSer *= PatentParticular.PatentSer) and
( cpt_records.ActivityInstanceSer = ActivityInstance.ActivityInstanceSer ) and
(cpt_records.ActivityInstanceRevCount = ActivityInstance.ActivityInstanceRevCount ) and
( ActivityInstance.ActivityInstanceSer = ActivityCapture.ActivityInstanceSer ) and
( ActivityInstance.ActivityInstanceRevCount = ActivityCapture.ActivityInstanceRevCount ) and
( Patent.PatentSer = Template.PatentSer ) and
( Template.TemplateSer = TemplateCycle.TemplateSer ) and
( TemplateCycle.TemplateCycleSer = ActivityInstance.TemplateCycleSer ) and
( Patent.PatentSer = PatentDoctor.PatentSer ) and
( PatentDoctor.ResourceSer = Doctor.ResourceSer ) and
( ActivityCapture.ActivityCaptureSer = ActCaptDiagnosisMH.ActivityCaptureSer ) and
( ActivityCapture.ActivityCaptureRevCount = ActCaptDiagnosisMH.ActivityCaptureRevCount ) and (cpt_records.CompletedDateTime >= @startdate ) and
( cpt_records.CompletedDateTime < dateadd(dd,1,@enddate) ) and
( cpt_records.ObjectStatus = 'Active' ) and
( PatentDoctor.OncologistFlag = 0 ) and
( PatentDoctor.PrimaryFlag = 1 ) and
( ActCaptDiagnosisMH.DiagnosisSer *= Diagnosis.DiagnosisSer) and
( ProcedureCodeMH.ProcedureCode like '%77781%' or ProcedureCodeMH.ProcedureCode like '%77782%' or ProcedureCodeMH.ProcedureCode like '%77783%' or ProcedureCodeMH.ProcedureCode like '%77784%' or ProcedureCodeMH.ProcedureCode like '%77785%' or ProcedureCodeMH.ProcedureCode like '%77786%' or ProcedureCodeMH.ProcedureCode like '%77787%')