Compare Every Record from Two Tables - sql

Assuming I have two SQL tables consisting of a single column,
i.e.
Table 1 Table 2
a1 a2
b1 b2
c1 c2
Is there a succinct SQL command to compare each record in one table against each record in the other? (and return true if any record from table 1 matches any record from table 2)
i.e.
if( a1 = a2 OR a1 = b2 OR a1 = c2 OR b1 = a2 OR b1 = b2...)
I want
If any record from table a matches table b (i.e., in a table of ints, they are the same int), return true.

Why not simply
if exists (select 1 from T1 inner join TB on T1.Col = T2.Col)

A full join is well suited to finding differences. To find rows that are missing in either table:
select *
from t1
full join
t2
on t1.col1 = t2.col1
where t1.col1 is null
or t2.col1 is null
This assumes that the single column is unique (i.e. has no duplicate values.)

Related

Is there a way of switching/changing the "ON" condition in a SQL query when the columns do not match?

I have two tables (t1 and t2). I want to print/keep all records from t1 (we'll treat t1 as the left table). I want to perform a JOIN with t1 and t2 on two columns, but there's a problem. Table t1 consists of columns c1 and c2. Table t2 consists of columns c3, c4, and c5. I want to perform a JOIN between t1 and t2 on c1 (from t1) and c3 (from t2), but I also want to do a JOIN between t1 and t2 on c2 (from t1) and c4 (from t2) if records from c1 and c3 do not match.
Here are the two tables. Completely fictitious, but applicable
to my real, work-related problem
The table below is what I want
All the records/rows from t1 are printed.
I greatly appreciate anyone who comes forward with query solutions. With that being said, is there a way to solve this problem without UNION? Also, I am using SQL Server.
Looking at the input and output data, I think you mean to compare c2 and c4 if c1 and c3 differ, not c1 and c2. I recreated the tables in sql and this code below gives the result you're looking for.
In that case you can just join and use an OR:
SELECT
*
FROM
t1
LEFT JOIN t2 ON
t1.c1 = t2.c3
OR t1.c2 = t2.c4;

Select entries that have non repeating values on a specific column (although other columns may have repeating or non repeating values) (SQL)

Let's say I have the following table:
A
B
C
D
a1
b1
c1
d1
a1
b1
c1
d2
a2
b2
c3
d3
a2
b2
c4
d3
I want to filter and see all four columns for entries that have the same value con column A but different on column C, so I get only this as a result:
A
B
C
D
a2
b2
c3
d3
a2
b2
c4
d3
I don't really care if values con columns B and D are the same or different, although I would like to have them in my table to do further analysis later.
Using the DISTINCT statement would give me all the columns as a result, as they all are different in some column, so that doesn't work for me.
I read some questions (like this one) and the answers recommended using the row_number() over(partition by...) clause, although the use they gave it doesn't quite fit my problem (I think), as it would also return the first row with a repeating value on column C.
Any ideas how this could be done?
You can use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.a = t.a and t2.c <> t.c
)
order by t.a;
You could use a self join
select t1.*
from t t1
join t t2 on t1.a=t2.a and t1.c<>t2.c

Create view from 2 tables with one table precedence with other table (mostly both tables are similar except one col)

Create view from 2 tables with one table precedence with other table (mostly both tables are similar except one col).
Need a view from 2 tables.
Table1 table contains below columns with values:
ValueOne = C11
ValueTwo= C12
ValueThree= C13
Table2 table contains below columns (extra id column compare to table1).
Id = 123
ValueOne = C11
ValueTwo= V12
ValueThree= C13
Table2 is precedence over Table1. When w query with ID, and if Id is not there it has to pick the values from Table1. If the Id is available then it has to pick the values from Table2.
For this I need a view to combine these 2 tables and when we query from view we need to get proper results.
example: 1) Select * from ViewName where ID=123
then in this case I have to get below values (from table2, as the ID exist in the table2):
Id = 123
ValueOne = C11
ValueTwo= V12
ValueThree= C13
2) Select * from ViewName where ID=01
in this case it has to get the below values (Id and other values from Table1,
as ID is not there in Table2:
Id = 01
ValueOne = C11
ValueTwo= C12
ValueThree= C13
You will need to use a join. Something like this should work
CREATE VIEW View_1 AS
select t2.*
from Table2 t2
Union ALL
Select t1.*
from table1 t1
LEFT JOIN Table2 t2 on t1.ID = t2.ID
where t2.ID is NULL
I did not test but this should give you a good start.

How to compare 2 SQL table for difference

given the following table, how can I have the difference between the 2 tables giving the fact that T143 has every rows duplicated:
T001.CODE T143.CODE
---- -----
A1 A1
A1
A2 A2
A2
A3
A4 A4
A4
Result should be A3 as its only present in T001 - I also need to display all column from T143.
Here's what I have do far but after a manual check of data there is mistake:
SELECT CODE FROM T001
EXCEPT
SELECT CODE FROM T143
thanks
A Full outer join should do the trick
SELECT DISTINCT COALESCE(T001.CODE,T143.CODE), T143.*
FROM T001 FULL OUTER JOIN T143
ON T001.CODE = T143.CODE
WHERE T001.CODE IS NULL OR T143.CODE IS NULL
EDIT: Since DISTINCT is mentioned in the tags, i added it to the query
You can always do this using exists:
select t1.code
from t001 t1
where not exists (select 1 from t143 t2 where t2.code = t1.code);
If you also want the columns from t2, then use union all to get them.
If you want all codes in both tables, just do:
select t1.code
from t001
union -- on purpose for duplicate removal
select t2.code
from t143;

SQL calculate difference between cell values

Hi i was wondering if anyone knows how i can calculate the difference between two tables in tsql. I dont mean finding which cells are different - i mean calulcating the numerical difference. eg - Table A has column1, column 2 and only one 1 row. A1 = 40, B1 = 30. Table B has column1, column 2 and only one 1 row. A1 = 25, B1 = 10. So how could i get (A1 = 15, B1 = 20) using TSQL?
Given that you have no way to join the tables, you'll need a Cartesian product of the two. Luckily since each table only has one record that's not a problem.
You do it like this:
SELECT
TableA.A1 - TableB.A1 AS A1,
TableA.B1 - TableB.B1 AS B1
FROM TableA, TableB
If you had more than one record in each table, this query would return a result for every pair of records in both tables. So if TableA had n records and TableB has m, the result will have n*m records.
SELECT a.column1 - b.column1 , a.column2 - b.column2
FROM a
CROSS JOIN
b
Free from my mind =)
Select
(CONVERT(int, T1.A1) - Convert(int, T2.A1)) as A1,
(CONVERT(int, T1.B1) - Convert(int, T2.B)) as B1
From Table1 T1
inner join Table2 T2 on T1.Key = T2.Key