1

I'd like to get the distinct values that occur in either of two columns (or in both). Not the grouping of the columns, but the individual values themselves.

For example, querying a table of air travel routes with columns ORIGIN and DESTINATION. I'd like to get the list of all airports, regardless of whether it is an origin or destination.

SFO, LGA SFO, LAX JFK, LAX JFK, SFO LAX, SFO LAX, LGA 

Should return

LAX, SFO, LGA, JFK 

I am using Postgres 9.6 if it makes any difference.

3 Answers 3

4

Just use union:

select origin as airport from routes union -- used on purpose to remove duplicates select destination from routes; 
Sign up to request clarification or add additional context in comments.

Comments

0

While writing my question, I managed to solve it, but I'm not sure if this is the most optimal case.

SELECT DISTINCT orig AS airport_code FROM ( SELECT origin FROM airport_routes UNION ALL SELECT destination FROM airport_routes ); 

Comments

0

look into using aggregate functions (<,>) on the both columns

Comments