Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, you can get rid of the second LEFT JOIN.</p> <p>Your WHERE was removing out any matches, anyhow... For instance, if S.OrderID was 1 and there was a R.OrderID with a value of 1, the IS NULL enforcement in the WHERE wouldn't allow it. So it'll only return records where s.OrderID IS NULL, if I'm reading it correctly...</p> <p>Secondly, if you're dealing with a large amount of data, adding on a NOLOCK table hint typically won't hurt. Assuming you don't mind the possibility of a dirty-read here or there :-P Usually worth the risk, though.</p> <pre><code>SELECT * FROM [StaffEntry] s (nolock) LEFT JOIN [MainFrame] m (nolock) ON m.ItemNumber = s.ItemNumber AND m.Customer=s.Customer AND m.CustomerPO = s.CustomerPO -- purchase order AND m.CustPORev = s.CustPORev -- PO revision number WHERE s.EntryDate BETWEEN @StartDate AND @EndDate AND s.OrderID IS NULL </code></pre> <p>Lastly, there was a part of your question which wasn't too clear for me...</p> <blockquote> <p>"since I'm looking for records in the MainFrame table that don't exist, after doing the JOIN we have that ugly IS NULL in the where clause."</p> </blockquote> <p>Ok... But are you trying to limit it to just where those MainFrame table records don't exist? If so, you'll want that expressed in the WHERE as well, right? So something like this...</p> <pre><code>SELECT * FROM [StaffEntry] s (nolock) LEFT JOIN [MainFrame] m (nolock) ON m.ItemNumber = s.ItemNumber AND m.Customer=s.Customer AND m.CustomerPO = s.CustomerPO -- purchase order AND m.CustPORev = s.CustPORev -- PO revision number WHERE s.EntryDate BETWEEN @StartDate AND @EndDate AND s.OrderID IS NULL AND m.ItemNumber IS NULL </code></pre> <p>If that's what you were intending with the original statement, perhaps you can get rid of the s.OrderID IS NULL check?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload