This is using Postgres 12
I have a table that stages an import from a spreadsheet. It has a structure like
code | yes | no
XX1000 | yes | null
ZX1001 | null | no
I am trying to get all the results in a query so I can do other stuff with it.
When I run
select substring(code, 3), 'Y' from table1 where yes = 'yes' and no is null
I get the correct number of results (lets say 100).
When I run
select substring(code, 3), 'N' from table1 where yes is null and no is not null
I get the proper number of results (lets say 6)
If I run
select substring(code, 3), 'Y' from table1 where yes = 'yes' and no is null
UNION
select substring(code, 3), 'N' from table1 where yes is null and no is not null
I get 102 results, with 4 results missing from the yes query. Extracting all the results and comparing in Excel, I can see that there are not any values in the substring results that duplicate for both queries (e.g. each id is in the result set of the yes query once). I can also guarantee there are no overlapping substring(code, 3) values since the spreadsheet is populated from a different system where the values after the two characters are the id column in the table (I also verified the second query run separately returns distinct values compared to the first query).
Running a UNION ALL gives me 106 results that are all unique.
What is going on here? I am so confused why the UNION is dropping unique results.