0

I have two tables tbl_backupchecks and tbl_backupchecks_sqlbak.

Both tables have two column names, Company and ServerName.

I'd like to return a result set comprising of Company and Servername but in a merged list. Essentially if there is a servername and companyname in tbl_backupchecks_SQLBak that is not in tbl_backupchecks I want to report on it as well.

In basic terms I want a resultset of rows that is two column; company and servername. I tried writing a left join but it bought back two columns called ServerName. Can this be done?

Trinitrotoluene
  • 1,388
  • 5
  • 20
  • 39

8 Answers8

5

Assuming that your columns Company and Servername are datatypes that can be DISTINCT, you could use a UNION query:

SELECT Company, Servername FROM tbl_backupchecks
UNION
SELECT Company, Servername FROM tbl_backupchecks_sqlbak
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
0

I think a union should do that for you

   SELECT Company, ServerName
   FROM tbl_backupchecks
   UNION
   SELECT Company, ServerName
   FROM tbl_backupchecks_sqlbak
SGB
  • 616
  • 6
  • 10
0

Use UNION:

SELECT  Company, ServerName 
  FROM  tbl_backupchecks 
UNION
SELECT  Company, ServerName 
  FROM  tbl_backupchecks_sqlbak 
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
0

I think you want a UNION:

For distinct rows between both tables use this:

SELECT Company,ServerName
FROM tbl_backupchecks
UNION
SELECT Company,ServerName
FROM tbl_backupchecks_sqlbak

For all rows from both tables (will not remove duplicate rows that exist in both tables) use this:

SELECT Company,ServerName
FROM tbl_backupchecks
UNION ALL
SELECT Company,ServerName
FROM tbl_backupchecks_sqlbak
squillman
  • 13,363
  • 3
  • 41
  • 60
0
SELECT Company, ServerName FROM tbl_backupchecks

UNION

SELECT Company, ServerName FROM tbl_backupchecks_sqlbak

(Change UNION to UNION ALL if you want to keep the duplicates.)

MatBailie
  • 83,401
  • 18
  • 103
  • 137
0

If you only want unique records from both, use this:

SELECT DISTINCT Company, ServerName
FROM
(
SELECT Company, ServerName FROM tbl_backupchecks
UNION
SELECT Company, ServerName FROM tbl_backupchecks_sqlbak
) A
bv8z
  • 965
  • 2
  • 9
  • 19
0

I think you want a union of the 2 tables. Something like:

SELECT Company, ServerName FROM tbl_backupchecks
UNION 
SELECT Company, ServerName FROM tbl_backupchecks_SQLBak
Justin
  • 84,773
  • 49
  • 224
  • 367
0

Here is a solution that also tell you where the record is

select Company, ServerName, sum(case when is_bak=0 then 0 else 1) as in_bak, sum(case when is_bak=0 then 1 else 0) as in_not_bak from (
select Company, ServerName,0 as is_bak from tbl_backupchecks union 
select  SELECT Company, ServerName, 1 as is_bak FROM tbl_backupchecks_SQLBak
) group by Company, ServerName
idanzalz
  • 1,740
  • 1
  • 11
  • 18