Solved

Left Join Not returning all records required.

Posted on 2004-09-22
4
422 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you try my suggestion?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

743 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

16 Experts available now in Live!

Get 1:1 Help Now