How to find differences in a content table SQL - sql

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....

Related

Can I replace Union all with any kind on joins?

I have the below query for negative testing, But I want to replace the union all if possible.
select A.*
from A
join B
on A.COL1=B.COL1
where B.COL3 is null
union all
select A.*
from A
join B
on A.COL2=B.COL4
where B.COL5 is null;
Need to get data from both SQL without using union all
You could combine the two queries into a single join and collapse the where condition into it:
select A.*
from A
join B on (A.COL1 = B.COL1 and B.COL3 is null) or
(A.COL2 = B.COL4 and B.COL5 is null)
Since you're only after data from Table A you don't need the join to table B at all and can re-write this as an Exists...
SELECT A.*
FROM A
WHERE EXISTS (SELECT 1
FROM B
WHERE A.COL1=B.COL1 and B.COL3 is null)
OR EXISTS (SELECT 1
FROM B
WHERE A.COL2=B.COL4 and B.COL5 is null)
But this has likely has two issues:
I'm pretty sure if you look at the execution plan for both; you'll find the union all is more efficient because it operates at a set level instead of a row level ad the OR needed in this is slower.
This will return 1 record from A instead of 2 from that of a union all. had it been a union; this should/would return the same results and avoid the union. But simply put you want the same data from A twice (or more depending on cardinality of joins)
SELECT A.*
FROM A
JOIN B ON (A.COL1 = B.COL1 OR A.COL2 = B.COL4) AND B.COL3 IS NULL;

filtering for column pairs in SQL

consider two tables that have the following columns
A
1,X
2,Y
3,Z
B
1,X
1,Y
1,Z
2,X
2,Y
2,Z
3,X
3,Y
3,Z
is it possible select rows in B that have column pairs as in A without joining or a third column?
something like
select * from B where distinct columns in (select distinct columns from A)
You could use exists logic:
SELECT col1, col2
FROM TableB b
WHERE EXISTS (SELECT 1 FROM TableA a WHERE a.col1 = b.col1 AND a.col2 = b.col2);

Oracle SQL CREATE TABLE takes too long time or not able to create (without CREATE TABLE works)

Any one can give some input here.
I am using this structure to create the table,
create table user.sales_fill as
select
a.name
,gender
,a.age
,b.sales
, 1 as logic
from
(select distinct
name, age, gender
from cust_info )a
left join
sales b
on
a.age = b.age
and a.gender = b.gender
;
when I only use the SELECT part it only takes 7.5seconds to showing the results.
select
a.name
,gender
,a.age
,b.sales
, 1 as logic
from
(select distinct
name, age, gender
from cust_info
)a
left join
sales b
on
a.age = b.age
and a.gender = b.gender
;
but if i Add 'create table' on top of the this select code. I never able to get the table created.
i have right to create the table if i use the the following the table created (but not the right content)
create table user.sales_fill as
select
gender
,age
,sales
, 1 as logic
from sales
;
Any suggestion? Thank you!
Pre-create the table by adding where rownum = 0 to the end of the query. Then do separate inserts:
CREATE TABLE misery
AS
SELECT a.col1
, a.col2
, a.col3
, b.col4
FROM loves a
INNER JOIN company b ON (a.col1 = b.col1)
WHERE a.ROWNUM < 1;
INSERT INTO misery( col1
, col2
, col3
, col4 )
SELECT a.col1
, a.col2
, a.col3
, b.col4
FROM loves a
INNER JOIN company b ON (a.col1 = b.col1);
INSERT INTO misery( col1, col2, col3 )
SELECT col1, col2, col3
FROM andhow
WHERE NOT EXISTS
(SELECT NULL
FROM andhow
WHERE andhow.col1 = misery.col1
AND andhow.col2 = misery.col2
AND andhow.col3 = misery.col3)
Try to rewrite the subquery so that it doesn't need to explode the imploded rows (JOIN ON DISTINCT):
select
*
from
cust_info
join
sales
on
cust_info.age = sales.age
and cust_info.gender = sales.gender
union
select
name,
age,
gender,
null
from
cust_info
where
not exists(
select
*
from
sales
where
sales.age=cust_info.age and
sales.gender=cust_info.gender
)
This way it should not be confusing to the optimizer.
Also to note: the create table as could be slow because it duplicates indexes from the scanned tables. You can try create view (which also has the advantage of not taking space and auto-updating with the updated tables), or you can try an explicit create table without indexes and as... and only then insert into ... select ... .

Conditional joining in Postgres using levenshtein

I have two tables lets say Table A and Table B...
I want to query these two tables so that I can check to see if two columns in the tables say col1 and col2 are similar and show them.
Something like:
SELECT A.col1, B.col2
FROM A INNER JOIN B
ON LEVENSHTEIN(A.col1, B.col2) < 2;
Ultimately I want to also get rid of all the white spaces within and just look at the characters within the columns so
if col1 values where {g o o d, b a d,}
and col2 had {good,bad}
I would like those to be matches
Does this work?
SELECT A.col1, B.col2
FROM A INNER JOIN
B
ON LEVENSHTEIN(replace(A.col1, ' ', ''), replace(B.col2, ' ', '')) < 2;

Compare the data in two tables with same schema

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