I need to do a comparison from one table (which is a CRM inventory) to 4 other tables (which are webserver inventories). Currently I am using the following query, but I am getting duplicates and false positives due to the fact it is rechecking the first table against each other table and simply outputting the results every time then displaying them all together:
SELECT sc.Client,
ws.[Host Header],
ws.[IP Address],
ws.[Status]
FROM sc
FULL JOIN ws
ON sc.Urls = ws.[Host Header]
WHERE sc.Client IS NULL
OR ws.[Host Header] IS NULL
OR sc.Urls <> ws.[Host Header]
UNION
SELECT sc.Client,
ws2.[Host Header],
ws2.[IP Address],
ws2.[Status]
FROM sc
FULL JOIN ws2
ON sc.Urls = ws2.[Host Header]
WHERE sc.Client IS NULL
OR ws2.[Host Header] IS NULL
OR sc.Urls <> ws2.[Host Header]
UNION
SELECT sc.Client,
ws3.[Host Header],
ws3.[IP Address],
ws3.[Status]
FROM sc
FULL JOIN ws3
ON sc.Urls = ws3.[Host Header]
WHERE sc.Client IS NULL
OR ws3.[Host Header] IS NULL
OR sc.Urls <> ws3.[Host Header]
UNION
SELECT sc.Client,
ws4.[Host Header],
ws4.[IP Address],
ws4.[Status]
FROM sc
FULL JOIN ws4
ON sc.Urls = ws4.[Host Header]
WHERE sc.Client IS NULL
OR ws4.[Host Header] IS NULL
OR sc.Urls <> ws4.[Host Header]
Can someone point me in the right direction? I want to return only the distinct results from each of the tables that are null or mismatching in the sc.Client and ws.[Host Header] fields and there are no duplicates between the 4 webserver tables.
Thanks!
Why don't you try first to perform a UNION between all ws tables and then perform FULL JOIN of sc table against the table expression resulting from the UNION operation? – Giorgos Betsos
This was dead on! I adjusted my query to the following and everything worked great!
SELECT sc.Client, ws.[Host Header], ws.[IP Address], ws.[Status]
FROM sc full outer join (select * from ws1 union all select * from ws2 union all select * from ws3 union all select * from ws4) as ws ON sc.Urls = ws.[Host Header]
Thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.