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,338
  • 4
  • 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
  • 37,856
  • 8
  • 63
  • 74
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
  • 32,669
  • 4
  • 56
  • 80
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
  • 12,611
  • 3
  • 38
  • 58
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
  • 77,948
  • 17
  • 98
  • 134
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
flayto
  • 955
  • 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
  • 82,493
  • 48
  • 216
  • 356
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,735
  • 1
  • 10
  • 17