Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:14:34+00:00 2026-06-09T01:14:34+00:00

I am trying to rework a query so that it uses standard joins (in

  • 0

I am trying to rework a query so that it uses standard joins (in from clause) and not the old version (migrating to SQL server so forced to do this conversion). The problem I am running into is that my version of the query below returns a lot fewer records than the original. So I am thinking one of my outer joins isn’t so outer. Thanks for any help, especially if you actually make it through the whole query. I can’t seem to find any good documentation on how to make a transition like this.

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%')

the origional that brings back more records is


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%')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:14:37+00:00Added an answer on June 9, 2026 at 1:14 am

    The problem is that a left join returns nulls for the joined column values where no matching row is found, but you are checking those column values in the where clause, but the where predicates are is executed after all the rows are joined, so they’ll never match and your outer joins are sunk.

    ie, this example query:

    select *
    from table1 t1
    left join table2 t2 on t2.fk = t1.id
    where t2.col1 = 'x'
    

    will never return any rows that don’t have corresponding rows in table2, because col1 will be null, and comparisons with null are always false, except for col1 is null.

    To fix this, you need to move the tests into the ON clause, so the comparison happens while the join is being made, like this:

    select *
    from table1 t1
    left join table2 t2 on t2.fk = t1.id and t2.col1 = 'x'
    

    Now the left join will still return rows while matching on the key, and applying extra predicates to further refine the match.

    In your case, you are doing an outer (ie left) join to ProcedureCodeMH as PCMH, but testing the columns of PCMH in the WHERE clause, similar to the first example above.

    The rewrite of your query would then be (see in-line comment for where the change is):

    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
    

    Note: inner join can be simplified to simply join – inner is the default join type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
So I'm trying to make a client for my server in C#, that accepts
It's an old project that i'm updating so is not th prettiest of coding.
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
I'm trying to use the geocoding code from here in my ASP.NET MVC 2
Trying to randomly generate one div from an array on each load of a
trying to use the beforeSave hook with some server side validation and throwing a
Trying to loop thru a result that has items. Items have a type and
Trying to find a way to rapidly develop web services on the Mac Server
I am trying to improve my C++ by creating a program that will take

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.