I'm trying to compare two almost identical select queries; both queries output the same columns, from the same table. Only the conditions are different on a single attribute (where foo = 'BAR'
vs. where foo <> 'BAR'
). The results are huge and might have only a few subtle differences. My current approach is to execute both queries and then put the results into Excel and use formulas to compare the two. Tedious and poor - there must be a better way.
How can I build a query that performs each select separately and then shows me only those lines that don't exist on both sides?
Here is a simplified version of my queries:
-- first query
select a.foo, a.bar, a.moo from abc.mytable a where a.somedetail = 'BAR'
group by a.foo, a.bar, a.moo
order by a.foo, a.bar, a.moo;
-- second query
select a.foo, a.bar, a.moo from abc.mytable a where a.somedetail <> 'BAR'
group by a.foo, a.bar, a.moo
order by a.foo, a.bar, a.moo;
Looking at this answer, it looks like I need either the EXCEPT operator (but in both directions, is that possible?), or either a UNION or a FULL OUTER JOIN but for those I don't really know how to set the WHERE clauses to match all the columns in the two sets yes having differences in the somedetail
column.
This only needs to be executed once per database, but performance is very much a consideration because the data covers many millions of rows.