Combine columns from multiple tables into one

0

Sorry if this seems simple. Either I'm not asking the right question, or understanding the answers I've found so far. I have 12 temp tables, each created from a query. Every table has a column named 'ComputerName', and a second column with that query's results. Only 'ComputerName's are listed in each temp table where there was a result of a query. (Ex. tab1 has pc1 - tab1.data, pc3 - tab1.data, and pc5 - tab1.data. tab2 has pc1 - tab2.data, pc2 - tab2.data, and pc6 - tab2.data.) I want to combine the tables into one table. (Ex: Result would be: pc1 - tab1.data - tab2.data, pc2 - null - tab2.data, pc3 - tab1.data - null, pc5 - tab1.data - null, pc6 - null - tab2.data .) I realize I may need to start with putting two tables together, and then continue that process with the remaining 10. But I need a starting point. Any thoughts? Thanks.

If your initial table does not have all the computer names in it you could either get a distinct list on the fly (as in the example below), create another table that has all the computer names and use it as your primary SELECT FROM table, or possible create a SQL View that is the equivalent to my "on the fly method".

I'm a little confused by what you mean you say "put these tables together" but I think you're looking for one of two things. Either, you want to combine the results of all the tables, all at once, in which case you need to use the UNION or UNION ALL operator like this:

SELECT *
FROM tab1
UNION ALL
SELECT *
FROM Tab2
--etc.

This will put all these tables into a single result set. In this case I used UNION ALL, which just does a straight combination. If you just put in UNION, then you get a unique set of data, eliminating duplicates.

Or, you're trying to combine all the tables to get common data out of them, something like this:

Thanks Grant. Your second suggestion is close. I've tried that, but but with a Left or Full Outer Join. What I get is if Tab2.ComputerName is not in Tab1, Tab2.ComputerName with Tab2.data will not show up in the result. I think I need an Outer Join with a Union but I haven't been able to figure it out.