Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Left Join Not returning all records required.

Posted on 2004-09-22
4
Medium Priority
?
438 Views
Last Modified: 2006-11-17
I have a query.....

If a record in the left join doesn't meet the criteria, it omits the record from the record set, instead of just returning a stock value of 0....

How can I fix this please?   Long Query below:
-----------------------------------------
SELECT Count(Stock.[Item Number]) AS sumofqty, OrderDetails.[Order Number], OrderDetails.[Sequence Number], OrderDetails.[Item Number], OrderDetails.Description1, OrderDetails.Description2, OrderDetails.[Qty Ordered], OrderDetails.[Qty To Ship], OrderDetails.[Qty Shipped], OrderDetails.[Unit Price], OrderDetails.[Qty Return To Stock], OrderDetails.[Unit of Measure], OrderDetails.[Unit Weight], OrderDetails.[Taxable Flag], OrderDetails.[Select Code], OrderDetails.[GST Taxable Flag], OrderDetails.[Counter Type], OrderDetails.[Pick Sequence], OrderDetails.[Shelf Life], OrderDetails.[From Warehouse]

FROM OrderDetails LEFT JOIN Stock     ON OrderDetails.[Item Number] = Stock.[Item Number] WHERE (((Stock.Warehouse)="pk") AND ((Stock.[Date Invoiced]) Is Null) AND ((Stock.[Order Number]) Is Null))

GROUP BY OrderDetails.[Order Number], OrderDetails.[Sequence Number], OrderDetails.[Item Number], OrderDetails.Description1, OrderDetails.Description2, OrderDetails.[Qty Ordered], OrderDetails.[Qty To Ship], OrderDetails.[Qty Shipped], OrderDetails.[Unit Price], OrderDetails.[Qty Return To Stock], OrderDetails.[Unit of Measure], OrderDetails.[Unit Weight], OrderDetails.[Taxable Flag], OrderDetails.[Select Code], OrderDetails.[GST Taxable Flag], OrderDetails.[Counter Type], OrderDetails.[Pick Sequence], OrderDetails.[Shelf Life], OrderDetails.[From Warehouse], OrderDetails.[Order Number], OrderDetails.[Sequence Number]

HAVING (((OrderDetails.[Order Number])=109230))

ORDER BY OrderDetails.[Order Number], OrderDetails.[Sequence Number];

---------------------------------------------
Thanks,
0
Comment
Question by:Dan Flood
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 2000 total points
ID: 12128852
Change this:

FROM OrderDetails LEFT JOIN Stock     ON OrderDetails.[Item Number] = Stock.[Item Number] WHERE (((Stock.Warehouse)="pk") AND ((Stock.[Date Invoiced]) Is Null) AND ((Stock.[Order Number]) Is Null))

to:

FROM OrderDetails LEFT JOIN (SELECT * FROM Stock WHERE Stock.Warehouse="pk" AND Stock.[Date Invoiced] Is Null AND Stock.[Order Number] Is Null) As A ON OrderDetails.[Item Number] = A.[Item Number]

Change any references to the Stock table in the rest of the query to A.
0
 
LVL 12

Expert Comment

by:pique_tech
ID: 12128864
BIG hunch, not carefully checked:  You might be getting fouled by the HAVING clause.  Have you tried adding you Order Number criteria to the WHERE clause instead to see if that returns what you expect (without a HAVING clause altogether)?
0
 

Author Comment

by:Dan Flood
ID: 12128881
Hi,

It happened without a having clause, but i just added that so when I am running the query it will limit the about of records... with or without it it is the same... but thanks!  I just tried it again.
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12128970
Did you try my suggestion?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question