Shaggy
Shaggy

Reputation: 5790

Determine values that are present in only one of two tables, or in both of them

I have two tables in a SQL Server 2008 database in which I want to find:

Code:

CREATE TABLE #_temp
(ATM INT, Fault INT)

CREATE TABLE #_temp1
(ATM INT, Fault INT)

INSERT INTO #_temp  VALUES (10,101), (11,101), (12,101), (12,101), (10,105), (13,101)
INSERT INTO #_temp1 VALUES (10,102), (11,101), (12,103), (12,100), (10,105), (13,101)

/* My Try

SELECT * FROM #_temp t RIGHT JOIN #_temp1 t1 ON t.ATM=t1.ATM AND t.Fault=t.Fault AND t.ATM IS NULL AND t.Fault IS NULL

SELECT * FROM #_temp t JOIN #_temp1 t1 ON t.ATM=t1.ATM AND t.Fault=t.Fault

*/

DROP Table #_temp
DROP Table #_temp1 

Upvotes: 2

Views: 484

Answers (4)

twoleggedhorse
twoleggedhorse

Reputation: 5048

To find values that exist in one table and not another you should use a where clause to determine the nulls:

Create Table #_temp
(ATM Int,Fault Int)

Create Table #_temp1
(ATM Int,Fault Int)

Insert Into #_temp Values(10,101),(11,101),(12,101),(12,101),(10,105),(13,101)
Insert Into #_temp1 Values(10,102),(11,101),(12,103),(12,100),(10,105),(13,101)


--Values Present in both Table

SELECT t.* 
FROM #temp t
    INNER JOIN #_temp1 t1
        ON t.[ATM Int] = t1.[ATM Int]
        AND t.[Fault Int] = t1.[Fault Int]

--Values Present in First Table But not in Second

SELECT t.* 
FROM #temp t
    LEFT JOIN #_temp1 t1
        ON t.[ATM Int] = t1.[ATM Int]
        AND t.[Fault Int] = t1.[Fault Int]
WHERE t1.[ATM Int] IS NULL

--Values Present in Second Table But not in First

SELECT t.* 
FROM #_temp1 t
    LEFT JOIN #temp t1
        ON t.[ATM Int] = t1.[ATM Int]
        AND t.[Fault Int] = t1.[Fault Int]
WHERE t1.[ATM Int] IS NULL

Upvotes: 2

rharrison33
rharrison33

Reputation: 1282

Renaming the columns as tables should work:

SELECT A.ATM AS ATM1, A.FAULT AS FAULT1, B.ATM AS ATM2, B.FAULT AS FAULT2
FROM #_temp as A, #_temp1 as B
ON A.ATM = BUT.ATM

SELECT * FROM #_temp

SELECT * FROM #_temp1

Upvotes: 0

bummi
bummi

Reputation: 27377

no exact shure what you are searching, another one

Create Table #_temp
(ATM Int,Fault Int)

Create Table #_temp1
(ATM Int,Fault Int)

Insert Into #_temp Values(10,101),(11,101),(12,101),(12,101),(10,105),(13,101)
Insert Into #_temp1 Values(10,102),(11,101),(12,103),(12,100),(10,105),(13,101)


Select a.*,t.ATM as tATM,t.Fault as tFault,t1.ATM as t1ATM,t1.Fault as t1Fault from
(
Select t.* From #_temp t 
UNION
Select t.* From #_temp1 t 
) a
LEFT join #_temp t on t.ATM=a.ATM and T.Fault = a.Fault
LEFT join #_temp1 t1 on t1.ATM=a.ATM and T1.Fault = a.Fault 


Drop Table #_temp
Drop Table #_temp1 

Upvotes: 0

bummi
bummi

Reputation: 27377

Insert Into #_temp Values(10,101),(11,101),(12,101),(12,101),(10,105),(13,101)
Insert Into #_temp1 Values(10,102),(11,101),(12,103),(12,100),(10,105),(13,101)



Select t.* From #_temp t LEFT join #_temp1 t1 on t.ATM=t1.ATM and t.Fault is null
UNION
Select t.* From #_temp1 t  left join #_temp t1 on t.ATM=t1.ATM and t.Fault is NULL



Drop Table #_temp
Drop Table #_temp1 

Upvotes: 1

Related Questions