我正在尝试重新编写查询,以便它使用标准联接(在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%')
答案 0 :(得分:0)
问题是左连接为没有找到匹配行的连接列值返回空值,但是您正在检查where
子句中的那些列值,但where
谓词是在所有行被连接之后执行,因此它们永远不会匹配,并且您的外连接会被沉没。
即,此示例查询:
select *
from table1 t1
left join table2 t2 on t2.fk = t1.id
where t2.col1 = 'x'
将从不返回table2
中没有相应行的任何行,因为col1
将为null
,并与{{1}进行比较} null
,false
除外。
要解决此问题,您需要将测试移到col1 is null
子句中,以便在进行连接时进行的比较,如下所示:
ON
现在左连接仍会在匹配键时返回行,并应用额外的谓词来进一步细化匹配。
在您的情况下,您正在对select *
from table1 t1
left join table2 t2 on t2.fk = t1.id and t2.col1 = 'x'
进行外部(left
)联接,但在ProcedureCodeMH as PCMH
子句中测试PCMH
的列,类似于上面的第一个示例
然后重写您的查询(参见内容评论,了解更改的位置):
WHERE
注意: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
-- MOVED THIS TEST FROM WHERE CLAUSE TO HERE
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%')
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
可以简化为inner join
- join
是默认的连接类型。