Quote:
Originally Posted by AndyJay
Can someone help with the logic of this one please?
|
i was going to ask what the heck a row is doing in the clientproducts table with a clientid of zero, but decided that i'd rather not
let's just look at the cardinality of your joins
we'll start with the WHERE clause, and discover that you're looking for all products in category 15
so, since each product belongs to only one category, this join is okay
(aside: since you are looking for all products in category 15, i would write the FROM clause starting with the categories table, then join to the products table, because this represents the "intent" of the query, so it's more logical, and you did want comments on your logic, right? but as these are all inner joins, this is really just a question of style, and although style is very important, it doesn't affect this particular problem)
then you have the join from products to clientproducts
based on your keys, a product can belong to more than client, but you have this restriction for "client zero" so once again this join is okay, as it will return only one row per product
but when you add the clientoffers table, it "messes up the results"
this is because a product can belong to multiple offers by multiple clients
however, in your join, you have not ensured that you're bringing in only the offers for "client zero" and this is my guess as to the reason for the mess
add the following condition to the join --
Code:
AND dbo.ClientOffers.ClientID = dbo.ClientProducts.ClientID