Solved

Left Join Not returning all records required.

Posted on 2004-09-22
4
424 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:jakyll
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 500 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:jakyll
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now