sql select giving wrong data

I have a query that is not returning what I expected - why?  why is it returning the -1
SELECT j.JobID,jde.ToolNumber,'TN' as ToolType, j.DefaultLocationIndex as 'Location' 
FROM   Job j
LEFT JOIN JobDownholeEquipment jde ON j.JobID = jde.JobID 
       WHERE j.JobInProgress= 'true' 
             OR j.JobInitiated= 'true'
             AND jde.PickedUp = 0 
             AND j.DefaultLocationIndex = 4
             [embed=file 904777]

Open in new window

Capture.PNG
LVL 6
r3nderAsked:
Who is Participating?
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
No idea, unless you can give us a sample data source to work with.

A likely cause is that your WHERE clause has OR's and AND's it it, and I don't see any parentheses marks ( ) to define order of precedence.  For example, (2 + 3) * 4 = 20, and 2 + (3 * 4) = 14.

I see 'AND j.DefaultLocationIndex = 4' at the end, but the use of OR may not apply that to every row..
0
 
r3nderAuthor Commented:
SELECT j.JobID,jde.ToolNumber,'TN' as ToolType, j.DefaultLocationIndex as 'Location',jde.PickedUp
FROM   Job j
LEFT JOIN JobDownholeEquipment jde ON j.JobID = jde.JobID
       WHERE( j.JobInProgress= 'true'
             OR j.JobInitiated= 'true')
             AND jde.PickedUp = 0
             AND j.DefaultLocationIndex = 4
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.