I want to compare the contents of two tables in SQL (postgres). The tables have a matching schema. As output I want those rows that are different. I already checked various approaches (also some posted here on Stackoverflow), but to me it looks like I have a problem when fields in both columns are NULL. Here is what I came up with so far:
SELECT dipi.id AS dipi_id,dipi.foo AS dipi_foo,dipi.bar AS dipi_bar, rvdii.id AS rvdii_id,rvdii.foo AS rvdii_foo,rvdii.bar AS rvdii_bar
FROM schema1.mytable dipi
FULL OUTER JOIN schema2.mytable rvdii on dipi.id = rvdii.id AND dipi.foo = rvdii.foo AND dipi.bar = rvdii.bar
WHERE dipi.id is null or rvdii.id is null
So do a full outer join on all columns and then return those where one of the IDs is null.
But with this query I get back this with some sample data:
dipi_id dipi_foo dipi_bar rvdii_id rvdii_foo rvdii_bar 3 2016-01-01 NULL NULL NULL NULL NULL NULL NULL 3 2016-01-01 NULL
As you can see the "dipi_bar", "rvdii_bar" columns contains a null value, which don't match in a join (as I understood this). So in the case above I don't want any output, I want that when both columns/fields are NULL, that they get "matched" in the join (or ignored in the join, whatever works). What is the trick to do this?
Edit: Of course the column is not always null, so deleting from the join is not an option :)