|
Sorry if this seems simple. Either I'm not asking the right question, or understanding the answers I've found so far.
(comments are locked)
|
|
Grant beat me to it but I believe you are simply wanting to LEFT JOIN all of your tables ...
... will result in the following output ...
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".
SELECT C.ComputerName, T1.Data, T2.Data, T3.Data, T4.Data
FROM ( SELECT DISTINCT ComputerName FROM #TMP1
UNION
SELECT DISTINCT ComputerName FROM #TMP2
UNION
SELECT DISTINCT ComputerName FROM #TMP3
UNION
SELECT DISTINCT ComputerName FROM #TMP4 ) C
LEFT JOIN #TMP1 T1
ON C.ComputerName = T1.ComputerName
LEFT JOIN #TMP2 T2
ON C.ComputerName = T2.ComputerName
LEFT JOIN #TMP3 T3
ON C.ComputerName = T3.ComputerName
LEFT JOIN #TMP4 T4
ON C.ComputerName = T4.ComputerName
Thanks John. That's really close, but T2.ComputerName will only show up if there is a match to T1.ComputerName. I want to join T1, T2, T3, etc, on ComputerName if there is a match. But if there is no match of ComputerName, I still need to get that data in the resulting table. So, adding to your example above, PC6 could be null, null, null, FFF. Meaning the first time PC6 appears would be coming from T5 (no previous matches in T's 1-4).
Apr 07 '10 at 02:51 PM
gjl15
See my additional example based on your latest post about T1 not having all of the ComputerNames in it. I believe that should take care of your problem. ~ John
Apr 07 '10 at 03:13 PM
John Franklin
THAT IS IT !!!!! THANKS JOHN !!!! I just did this against 4 of the temp tables and 1200 rows just shot out, 5 columns wide. Thank you very much!
Apr 07 '10 at 03:31 PM
gjl15
I just realized myself before your post came on, that I needed to UNION the [ComputerName]s from all the table first. Then I could join each temp table's distinct collection of [ComputerName]s against a distinct master list. Thanks again! You saved me from my plan B, which was very ugly.
Apr 07 '10 at 03:35 PM
gjl15
Nice job John. I didn't understand what we were shooting for well enough.
Apr 07 '10 at 03:37 PM
Grant Fritchey ♦♦
(comments are locked)
|
|
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:
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:
But I'm not sure that's what you mean. 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.
Apr 07 '10 at 02:41 PM
gjl15
Clearly, John has it right. Sorry I didn't get what you were driving towards.
Apr 07 '10 at 03:38 PM
Grant Fritchey ♦♦
(comments are locked)
|
|
From what I gather, you should just be able to change the join to a full join in Grant's example (expanded to include all of your tables of course.). This article on MSDN may help: http://msdn.microsoft.com/en-us/library/ms187518.aspx
(comments are locked)
|
|
Hi, i want to retrieve my vtype_id and vtype_desc from 5 tables to view the activities of last 7 days on the basis of datediff. and my all table are having vtype_id. and also want to make it a union also. so pleaase tell me how to do. plz make it fast i have to submit my project 2day.
(comments are locked)
|
|
update Table1 Set (Select Column1 From Table1 Where ColumnID1=Table1.ColumnID1)='11', (Select Column2 From Table2 Where ColumnID2=Table1.ColumnID1)='22' Where Table1.ColumnId1=1
(comments are locked)
|
1 2 next page »

