I have been doing a bit of searching for a while now on a particular problem, but I can't quite find this particular question
I have a rather unusual task to achieve in SQL:
I have two tables, say A and B, which have exactly the same column names, of the following form:
id | column_1 | ... | column_n
Both tables have the same number of rows, with the same id's, but for a given id there is a chance that the rows from tables A and B differ in one or more of the other columns.
I already have a query which returns all rows from table A for which the corresponding row in table B is not identical, but what I need is a query which returns something of the form:
id | differing_column
----------------------
1 | column_1
3 | column_6
meaning that the row with id '1' has different 'column_1' values in tables A and B, and the row with id '3' has different 'column_6' values in tables A and B.
Is this at all achievable? I imagine it might require some sort of pivot in order to get the column names as values, but I might be wrong. Any help/suggestions much appreciated.
Yes you can do that with a query like this:
WITH Diffs (Id, Col) AS (
SELECT
a.Id,
CASE
WHEN a.Col1 <> b.Col1 THEN 'Col1'
WHEN a.Col2 <> b.Col2 THEN 'Col2'
-- ...and so on
ELSE NULL
END as Col
FROM TableOne a
JOIN TableTwo b ON a.Id=b.Id
)
SELECT Id, Col
WHERE Col IS NOT NULL
Note that the above query is not going to return all the columns with differences, but only the first one that it is going to find.
You can do this with an unpivot -- assuming that the values in the columns are of the same type.
If your data is not too big, I would just recommend using a bunch of union all statements instead:
select a.id, 'Col1' as column
from a join b on a.id = b.id
where a.col1 <> b.col1 or a.col1 is null and b.col1 is not null or a.col1 is not null and b.col1 is null
union all
select a.id, 'Col2' as column
from a join b on a.id = b.id
where a.col2 <> b.col2 or a.col2 is null and b.col2 is not null or a.col2 is not null and b.col2 is null
. . .
This prevents issues with potential type conversion problems.
If you don't mind having the results on one row, you can do:
select a.id,
(case when a.col1 <> b.col1 or a.col1 is null and b.col1 is not null or a.col1 is not null and b.col1 is null
then 'Col1;'
else ''
end) +
(case when a.col2 <> b.col2 or a.col2 is null and b.col2 is not null or a.col2 is not null and b.col2 is null
then 'Col2;'
else ''
end) +
. . .
from a join b on a.id = b.id;
If your columns are of the same type, there is a slick method:
SELECT id,col
FROM (SELECT * FROM A UNION ALL SELECT * FROM B) t1
UNPIVOT (value for col in (column_1,column_2,column_3,column_4)) t2
GROUP BY id,col
HAVING COUNT(DISTINCT value) > 1
If you need to handle NULL as a unique value, then use HAVING COUNT(DISTINCT ISNULL(value,X)) > 1 with X being a value that doesn't occur in your data
Related
I have 2 tables A & B with columns 6 columns in each table.
Table A has lesser rows than table B.
I want to write a Select Statement where if the below condition satisfies
-----A.Col1=B.Col1 and A.Col2=B.Col2 and A.Col3=B.Col3 and A.Col4=B.Col4 and A.Col5=B.Col5 and A.Col6=B.Col6-----
if all conditions are satisfied then in a new column say "Match" else "NoMatch"
How do I do that ?
I would suggest using exists. If you want a new column in A:
select a.*,
(case when exists (select 1
from b
where A.Col1 = B.Col1 and A.Col2 = B.Col2 and
A.Col3 = B.Col3 and A.Col4 = B.Col4 and
A.Col5 = B.Col5 and A.Col6 = B.Col6
)
then 'match' else 'nomatch'
end) as flag
from a;
Note: If you want the new column on B, the logic is the same but the two tables are reversed.
If any of the columns can have NULL values, then you need to take that into account.
You can use CASE statement and I am considering that you need all data from tableB and matching data from tableA as follows:
select b.*, a.*,
case when A.Col1=B.Col1 and A.Col2=B.Col2
and A.Col3=B.Col3 and A.Col4=B.Col4
and A.Col5=B.Col5 and A.Col6=B.Col6
then 'Match'
else 'No match'
end as res
from tableB b
left join TableA a
on A.Col1=B.Col1 and A.Col2=B.Col2
and A.Col3=B.Col3 and A.Col4=B.Col4
and A.Col5=B.Col5 and A.Col6=B.Col6
I full joined 2 tables first and then full joined a 3rd table, now I got 1000 more rows in the result. But when I added a where statement following the join process I can only get 200 more rows and it seems that some expected rows were filtered. I don't know what I've done wrong.
proc sql;
create table ECG.RECON as
select a.SUBJID as SUBJID_004 ,
a.VISIT as VISIT_004,
input(a.EGDAT, yymmdd10.) as EGDAT_004 ,
...
b.SUBJID as SUBJID_001 ,
...
c.DSDECOD
from
SOURCE.A a full join SOURCE.B b on
(a.SUBJID = b.SUBJID and a.VISIT = b.VISIT )
full join SOURCE.C as c on b.SUBJID = c.SUBJID
where c.EPOCH = "SCR" and c.DSDECOD ne "FAILURE" and a.TEST = "Inter";
quit;
Your where clause is causing empty rows to be filtered. Consider a simplified schema:
TableA
Col1 Col2
----------------
1 A
2 B
TableB
Col1 Col2
----------------
1 X
3 Y
And a simple full join with no filter:
SELECT *
FROM TableA AS A
FULL JOIN TableB AS B
ON A.Col1 = B.Col1
Which will return
A.Col1 A.Col2 B.Col1 B.Col2
---------------------------------------
1 A 1 X
2 B NULL NULL
NULL NULL 3 Y
Now, if you apply a filter to anything from A, e.g. WHERE A.Col1 = 1, you'll get rid of the 2nd Row (probably as intended) since 2 <> 1, but you'll also remove the 3rd row, since A.Col is NULL, and NULL <> 1. As you have removed all rows with no matching record in TableA you have effectively turned your full join into a left join. If you then apply a further predicate on TableB, your left join becomes an inner join.
With Full joins, I find the easiest solution is to apply your filters before the join by using subqueries, e.g.:
SELECT *
FROM (SELECT * FROM TableA WHERE Col1 = 1) AS A
FULL JOIN TableB AS B
ON A.Col1 = B.Col1;
Which removes the 2nd row, but still retains the 3rd row from the previous results:
A.Col1 A.Col2 B.Col1 B.Col2
---------------------------------------
1 A 1 X
NULL NULL 3 Y
You can also use OR, but the more predicates you have the more convoluted this can get, e.g.
SELECT *
FROM TableA AS A
FULL JOIN TableB AS B
ON A.Col1 = B.Col1
WHERE (Col1 = 1 OR A.Col1 IS NULL);
I would like to find records which differ from eachother, based on different datasets in the same table, which are loaded on a different date.
So if one or more attributes(except from the key) differ from eachother from dataset x loaded on 1-1-2018 and dataset y loaded on 31-12-2018.
How do i achieve this in SQL?
The key on which the compare should be made is ZIP_CODE + House_ID
Greets,
you can get previous zipcode by LAG
SELECT ZipCode, HouseId,
LAG(ZipCode, 1,0) OVER (ORDER BY LoadDate) AS ZipCodeMinus1,
LAG(HouseId, 1,0) OVER (ORDER BY LoadDate) AS HouseIdMinus11
FROM Addresses;
A simple way to compare sets is
select ... a
EXCEPT
select ... b
but you need another
select ... b
EXCEPT
select ... a
and this doesn't tell you which columns are different.
Or you use a full outer join:
select
coalesce(a.ZIP_CODE, b.ZIP_CODE)
,coalesce(a.House_ID, b.House_ID)
,case when a.col1 <> b.col then 'a: || a.col1 || ' b:' || b.col1 end
...
from
( select ....) as a
full join
( select ....) as b
on a.ZIP_CODE = b.ZIP_CODE
and a.House_ID = b.House_ID
and ( a.col1 <> a.col1 or
a.col2 <> a.col2 or
a.col3 <> a.col3 or
...
)
If columns are NULLable you need to add more conditions checking for one of both columns is NULL. Of course this comparison syntax can be automatically created using the existing metadata....
Maybe I need another coffee because this seems so simple yet I cannot get my head around it.
Let's say I have a tableA with a col1 where employee IDs are stored.... ALL employee IDs. And the 2nd table, tableB has col2 which lists all employeeID who have a negative evaluation.
I need a query which returns all ID's from col1 from table1 and a newcol which show a '1' for those ID's which do NOT exist in col2 of TableB.
I am doing this in dashDB
One option uses a LEFT JOIN between the two tables:
SELECT a.col1,
CASE WHEN b.col2 IS NULL THEN 1 ELSE 0 END AS new_col
FROM tableA a
LEFT JOIN tableB b
ON a.col1 = b.col2
Alternatively you can achieve your requirement with LEFT JOIN along with IFNULL function as below.
SELECT a.col1,
IFNULL(b.col2, 1) NewCol
FROM tableA a
LEFT JOIN tableB b
ON a.col1 = b.col2
If I have table A and table B, each with one column:
A:
col1
1
2
3
1
B:
col1
1
1
4
and I want all rows from A and the matching rows from B, only when the column has non null value in both tables, which one should I use?
select * from A left join B on A.col1 = B.col1 and A.col1 is not null AND B.col1 is not null;
select * from A left join B on A.col1 = B.col1 where A.col1 is not null OR B.col1 is not null;
select * from A left join B on A.col1 = B.col1 and (A.col1 is not null OR B.col1 is not null;)
My guess is that the first and the third are the same and will provide the desired output.
If you want to skip null values and you want to link both tables only on existing values you should use an INNER JOIN, the null check is redundant:
SELECT A.*
FROM A INNER JOIN B ON A.col1 = B.col1
NULL will never match any other value (not even NULL itself), unless the join condition explicitly uses the IS NULL or IS NOT NULL predicates.
In a comment you said you are checking for more than nulls in this case I would probaly take thederived table or CTE approach. Dervied table shown below as you did not specify which database backend, so I don't know if you can use CTEs.
select
from
(select from tablea where test is not null or test <>'' or test<>'N/A') a
JOin
(select from tableb where test is not null or test <>'' or test<>'N/A')b
ON a.col1 = b.col1
You just need
select * from A left join B on A.col1 = B.col1
NULL will never match anything (when not compared with IS NULLand the like), therefore NULL in A won't match anything in B.
Since you want all the rows from A, below query should work:
select * from A left outer join B on A.col1 = B.col1 where A.col1 is not null and A.col1<>'N/A' and A.col1<>''
http://sqlfiddle.com/#!2/98501/14