954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SQL Query join to include Null values where no item in second table

Hey guys,

Alright I have a tricky assignment to do in a view that will enable my next program to run MUCH faster and more reliable...

Now I need to list all Items (1 table) and their locations where they are stored (2nd table) in a way that enables 1 to transfer...

Problem is the second table does not contain all the necessary locations as the previous programs would delete a location entry once it hit 0...

So how do I do the following:

Table1:

Item ID
1. X
2. Y

Table 2:
Item ID Location Quantity
X A 5
Y B 3

Desired View:

Item ID Location Quantity
X A 5
X B 0
Y A 0
Y B 3


I realize that simply asking a solution without trying is not the aim of this forum... but I'm really at my wits end and I've tried all kinds of join Statements I know,

I dont think it will be too difficult, but I am just missing something

Would really appreciate a quick solution, thnx guys

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

Nevermind cant get the spacing right

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

Just for the record, you can do what you are seeking by introducing a third (which will hold all locations) in your join.
You need to inner join items with locations (or the available locations for each item if in your scope) and then left join this to table2 using both item and location as keys. In your select you should either select sum by quantity and group by item and location or select item, location, isnull(quantity,0).

adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: