|
-
Nov 25th, 2010, 01:06 AM
#1
[RESOLVED] Show all possible items in one table -- outer join thing?
This is a question pertaining to MSSQL 2008. Okay, I'm not sure how much information I have to give in order to get the point across. The basic idea is that I have a Stock table that holds 40 different items that can be added to an order for an outlet (store). I need to return all 40 stock items even if these stock items don't appear as on an order already, which is where I have a problem. I thought I could just outer join the Stock table or something, but I'm either not thinking clearly or just not seeing this. This is the query I've created to test, which returns only 27 records:
Code:
SELECT s.StockId
FROM OutletOrder oo
INNER JOIN OrderItem oi ON oo.OrderId=oi.OrderID
LEFT OUTER JOIN Stock s on oi.StockID=s.StockID
WHERE oo.OutletId=8 AND oo.RecieveDate IS NULL;
and, for testing purposes, this is a dump of the relevant tables and some data: http://davidmil.es/sql/dump.sql (~115k)
I would greatly appreciate any help!
-
Nov 25th, 2010, 01:54 AM
#2
Re: Show all possible items in one table -- outer join thing?
You say that you want every stock item but you have the Stock table on the right-hand side of a LEFT OUTER JOIN. A LEFT OUTER JOIN includes every record from the left-hand table and a RIGHT OUTER JOIN includes every record from the right-hand table.
-
Nov 25th, 2010, 02:01 AM
#3
Re: Show all possible items in one table -- outer join thing?
Thanks for the reply. Changing that join to a right outer join doesn't produce the desired result; I still only retrieve 27 records.
Any other insight?
Last edited by kows; Nov 25th, 2010 at 02:12 AM.
-
Nov 25th, 2010, 03:48 AM
#4
Re: Show all possible items in one table -- outer join thing?
You need to also alter the Where clause so that it doesn't force data to come from the oi/oo tables, eg:
Code:
WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
I personally would re-write it so that the table you always want data from is listed first in the From clause, eg:
Code:
SELECT s.StockId
FROM Stock s
LEFT OUTER JOIN OrderItem oi ON oi.StockID=s.StockID
LEFT OUTER JOIN OutletOrder oo ON oo.OrderId=oi.OrderID
WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
-
Nov 25th, 2010, 09:19 AM
#5
Re: Show all possible items in one table -- outer join thing?
Thanks for the reply again!
 Originally Posted by si_the_geek
I personally would re-write it so that the table you always want data from is listed first in the From clause, eg:
Code:
SELECT s.StockId
FROM Stock s
LEFT OUTER JOIN OrderItem oi ON oi.StockID=s.StockID
LEFT OUTER JOIN OutletOrder oo ON oo.OrderId=oi.OrderID
WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
I did this before because it made more sense to me, but I kept getting the same results. So I left it alone.
 Originally Posted by si_the_geek
You need to also alter the Where clause so that it doesn't force data to come from the oi/oo tables, eg:
Code:
WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
oo.OutletId isn't a nullable field, so OutletId will never be null, and neither is oi.OutletId/oi.StockId. Changing this doesn't do anything, but thank you for the suggestion.
Would anyone be open to running the SQL file I've provided in order to play around with this? Thanks again in advance!
-
Nov 27th, 2010, 05:52 PM
#6
Re: Show all possible items in one table -- outer join thing?
Would anyone happen to have any insight on this? I've still not been able to produce a desired result after much playing around.
-
Nov 28th, 2010, 10:27 PM
#7
Re: Show all possible items in one table -- outer join thing?
Not sure what you are trying to do, are you just trying to get to StockID's? What are you trying to get from the other tables? Do you just want to return 40 items?
-
Nov 29th, 2010, 12:49 AM
#8
Re: Show all possible items in one table -- outer join thing?
I need to return every StockID that exists in the Stock table, regardless of if that StockID exists in an OrderItem for an Order. So, 40 items need to be returned every time. The quantity and prices in the order and some other things will be calculated later on, but I simplified the query for posting on this forum.
-
Nov 29th, 2010, 08:37 AM
#9
Re: Show all possible items in one table -- outer join thing?
So why not just just select the StockId from the stock table? That will return all wither or not they have ever been ordered
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Nov 30th, 2010, 12:54 AM
#10
Re: Show all possible items in one table -- outer join thing?
 Originally Posted by GaryMazzone
So why not just just select the StockId from the stock table? That will return all wither or not they have ever been ordered
My thoughts also.
-
Nov 30th, 2010, 12:36 PM
#11
Re: Show all possible items in one table -- outer join thing?
But along with the stock items, the order information is needed to be displayed. What you're suggesting, after all, is what this query already does:
Code:
SELECT s.StockId
FROM Stock s
LEFT OUTER JOIN OrderItem oi ON oi.StockID=s.StockID
LEFT OUTER JOIN OutletOrder oo ON oo.OrderId=oi.OrderID
WHERE (oo.OutletId=8 AND oo.RecieveDate IS NULL) OR (oo.OutletId Is Null);
I'm not sure if you're not understanding the problem, or what, but I solved my own problem by only joining the OutletOrder table on multiple conditions (essentially, move WHERE clause to JOIN clause):
Code:
LEFT OUTER JOIN OutletOrder oo
ON oo.OrderId=oi.OrderID
AND oo.OutletId=8
AND oo.RecieveDate IS NULL
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|