I am using Sql 2k5. I have 2 tables thats are identical in all aspects except the data. I need to join the 2 tables using the same WHERE condition. How do i do it without using a UNION?
For example, the 2 tables are goodCustomer and badCustomer.
I would like to join the data in these 2 tables using a WHERE condition like
How would the query change if the data is mutually exclusive and if it isnt.
Any help is appreciated.
Why don't you wan't to use UNION? I am not sure what you want to accomplish, but have you tried INTERSECT or EXCEPT? I give you some samples for you to try.
This select will return every record from table1 where there are matching records in table2:
This select will return every record from table1 where there are no matching records in table2:
Another solution may be FULL OUTER JOIN, the following sample will give you every record from table1 and table2 that matches your where clause. If there records in table1 that does not have any records in table2, you will get NULL for the table2 columns, etc. :
answered Dec 02, 2009 at 04:57 AM
In addition to the other suggestions, since you're trying to combine the data from the two tables, you should probably be using UNION ALL. With that you won't see the aggregation functions that are normally associated with a UNION, so the performance should be very acceptable.
answered Dec 02, 2009 at 10:26 AM
Grant Fritchey ♦♦
Can I ask why you do not want to use a UNION? It seems that UNION, or as Grant suggested UNION ALL is exactly what you want if I understand the rest of the question correctly.
If the problem is that you do not want two separate sets of where conditions then you can use a subquery or CTE. For instance you could use something like:
answered Dec 02, 2009 at 02:00 PM