Hi there
I am trying to find out the most efficient way of joining some tables.
Table 1 - Products
fields - ProductID, ProductName
Table 2 - Shops
fields - ShopsID, Shopname, ShopCity
Table 3 - ShopsProductsLookup
fields - ShopsID, ProductsID, Quantity
What I want to end up with is a stored procedure that you pass in a ShopID to and it returns a result set of all the products (no duplicates). Each row should contain the productID, productname and, if the product has a joining record in the ShopsProductsLookup table for the passed in ShopID the row should also contain a 'true' marker (yes the product is that shop) and the quantity of that product type in that shop. If the product is not in the ShopsProductsLookup for that passed in ShopID it should instead have 'false' in the 'yes product is in the shop' column and quantity can be zero.
So I'm hoping to end up with a duplicate free result set containing something like
1 Shoes true 10
2 Hats false 0
3 Pants true 5
4 Shirts true 1
etc.
So far I can only get a result set back with duplicate rows like
1 Shoes true 10
1 Shoes false 3 (this row is because another shop has 3 shoes)
etc
(unless I do multiple sub-queries, but I assume that is not the best way performance wise as particularly given these table may get quite large)
Any help would be greatly appreciated.