I want to SELECT values based on a column called LOCATION, but I want to SELECT values if LOCATION has values for ‘TOWN’ if there are no values for LOCATION ='TOWN' then SELECT values for LOCATION='CITY'.
There are always values in the table for LOCATION='CITY' but we want to get values from the table for LOCATION='TOWN' and if this returns no values then fall back to LOCATION='CITY'.
Can this be done in the same (single) SELECT.
I don’t think this can be accomplished using a simple DECODE.
This seems better suited to a function, but it is possible (at least, based on my understanding of your question).
I’ve created a sql fiddle to demo it.
Replace City/Town with your values or IDs. The
EXISTSclause searches for your first level search (city, based on your question) – if that fails, it falls back to matching against theelsecondition (town, in this case).