Can someone please help me to find out the problem with the following Self Join using Full Outer Join
SELECT
T1.product, T1.vendor, T2.product, T2.vendorFROM ( product_vendor T1 FULL OUTER JOIN product_vendor T2 ON T1.vendor = T2.vendor )
WHERE
T1.product = 1 AND T2.product = 2This query always returns only the matching rows (as if INNER join). Is there a way to get the non-matching rows too, using a single query, without using temp tables If I put the results for Product 1 and Product 2 in two different temp tables and then do a FULL OUTER JOIN on the temp tables, I could get the desired result. I tried putting the WHERE clause condition in the Join, having <> etc., - but, nothing worked.
Thanks.
Sample Data in product_vendor table (I need to compare ONLY two products at any time - in this example Product 1 and 2):
Product Vendor
1 101
1 102
1 103
2 101
2 103
2 104
3 101
4 102
.... ....
Desired Output:
T1.product T1.vendor T2.product T2.vendor
1 101 2 101 - Products 1 and 2 having same vendor
1 103 2 103 - Products 1 and 2 having same vendor
1 102 2 NULL - Vendor 102 having product 1 but no Product 2
1 NULL 2 104 - Vendor 104 having product 2 but no Product 1

SQL 2005 Transact-SQL Full Outer Self Join
Clark Anderson
aquaseal
When you have a [LEFT] JOIN, and you add columns from the [RIGHT] table in the WHERE clause, you effectively turn it into an INNER JOIN.
SO, move the [T2.product = 2] into the JOIN criteria.
bharathi_tunes
Thanks for the solution. The first option works just fine. However, the second one requires a minor change as mentioned below, and it works fine after the fix.
...
when p2 is null then 'Vendor sells p1 but not p2'
when p1 = 1 then 'Vendor sells p1 but not p2' <-- both p1 and p2 will be 1
HellsChicken
Thanks for the response. However, this query returns only the matching rows and non-matching rows for only one product (as if it is LEFT or RIGHT OUTER JOIN). I belive it is because of the WHERE clause. In case you are interested, the solution posted by another user (from MS team) works fine.
coolarian
"This will perform much faster than the FULL JOIN especially for larger data sets."
If there is an unique index on Product + Vendor (in that order, combination key), do you still think the second option will be more efficient than the first one This table is expected to have about 10,000 products and about 20 vendors for each product (about 200,000 at the maximum). Please clarify.
Thanks