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 3430666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:13:56+00:00 2026-05-18T07:13:56+00:00

I know that the issue is in my WHERE clause but I don’t have

  • 0

I know that the issue is in my WHERE clause but I don’t have a good solution to correct the problem.

if a entry in section 2 is not marked as deleted and is linked to a section 3 entry which is marked as deleted then I lose the entry altogether.

Any help would be appreciated.

p.s this same thing occurs from 3 to 4 as well
the table structure cannot be altered unfortunately … before my time.

Table       FK         PK         bit
------------------------------------------
Section1 |  JobID   | Sect1ID | isDeleted
Section2 |  Sect1ID | Sect2ID | isDeleted
Section3 |  Sect2ID | Sect3ID | isDeleted
Section4 |  Sect3ID | Sect4ID | isDeleted

Stored procedure:

    @JobId int
    AS
SET NOCOUNT ON

SELECT J.Section1,
    J.Section2,
    J.Section3,
    J.Section4,
    S1.Id AS Section1Id,
    S1.[Order] + 1 AS S1Order,
    S1.Text AS S1Text,
    S2.Id AS Section2Id,
    S2.Text AS S2Text,
    S2.[Order] AS S2Order,
    S3.Id AS Section3Id,
    S3.Text AS S3Text,
    S3.[Order] + 1 AS S3Order,
    S4.Id AS Section4Id,
    S4.Text AS S4Text,
    S4.[Order] AS S4Order
       FROM JT_Jobs J 
       JOIN JT_Section1 S1 ON J.JobId = S1.JobId
  LEFT JOIN JT_Section2 S2 ON S1.Id = S2.Section1Id
  LEFT JOIN JT_Section3 S3 ON S2.Id = S3.Section2Id
  LEFT JOIN JT_Section4 S4 ON S3.Id = S4.Section3Id
      WHERE J.JobId = @JobId 
        AND S1.IsDeleted = 0
        AND (s2.IsDeleted is null or s2.IsDeleted = 0) 
        AND (s3.IsDeleted is null or s3.IsDeleted = 0) 
        AND (s4.IsDeleted is null or s4.IsDeleted = 0)
   ORDER BY S1.[Order], S2.[Order], S3.[Order], S4.[Order]
  • 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-05-18T07:13:56+00:00Added an answer on May 18, 2026 at 7:13 am

    Put the join conditions in the join clause and not in the where clause

      From JT_Jobs J
         Join JT_Section1 S1 
             On S1.JobId = J.JobId
                And S1.IsDeleted = 0
         Left Join JT_Section2 S2
             On S2.Section1Id=S1.Id 
                And (s2.IsDeleted is null or s2.IsDeleted = 0)  
         Left Join JT_Section3 S3 
             On S3.Section2Id = S2.iD
                And (s3.IsDeleted is null or s3.IsDeleted = 0)  
         Left Join JT_Section4 S4
             ON S4.Section3Id = S3.Id
                And (s4.IsDeleted is null or s4.IsDeleted = 0)    
      Where J.JobId = @JobId  
    

    …. to explain, predicates (boolean conditions) in a where clause are only evaluated after all the joins have been processed.

    Joins are processed (logically) by examining both sides of the join, adding records into the resultset where all the join conditions are true, and then, if it’s an outer join, adding back the records from the outer side that did not have a matching record on the other side. So the join conditions are only applied to the inner side, and to the records on the outer side that match a record on the inner side.

    So putting a condition in the where clause causes it to be applied to the last intermediate resultset, after all joins have been processed. At this point all those outer records have been added back in, and for a condition which affects the outer side of an outer join, it will eliminate all those records that were added back in during the second half of that outer join, effectively turniug it back into an inner join.

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

Sidebar

Related Questions

I know that this kind of issue is most discussed. But I would like
I know that there are some threads have a similar issue with this thread.
Heyy can somebody help me ? i have this issue that still do know
I know there are other questions that have similar issues, but I have read
My issue is that I have a Select statement that has a where clause
I know that memory usage is a very complex issue on Windows. I am
I would like to know if there exist algorithms that solves this issue. It
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know there is this question that is very similar, but I wanted to

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.