SQL question, DISTINCT returned values in multiple UNION ALL'ed statements

StackOverflow https://stackoverflow.com/questions/1041316

  •  22-07-2019
  •  | 
  •  

Question

I have a sql statement that is a union of several queries which all just return keys:

SELECT DISTINCT key as KEY FROM tablea WHERE XYZ
    UNION ALL
SELECT DISTINCT id as KEY FROM tableb WHERE XYZ
    UNION ALL
...

My question is that there are some queries that return keys that overlap, and I actually want the final KEY field returned values to be distinct values. Any ideas?

Was it helpful?

Solution

Use UNION instead of UNION ALL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top