Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Left Join Not returning all records required.

Posted on 2004-09-22
4
431 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 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: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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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