János János - 2 months ago 9
SQL Question

SQL aggregate method return string if it is in set, or any other string if string is not in set

Looking for an aggregate method, or any smart approach that can result the following:

- SELECT MIN_DISTANCE(set, b) FROM {a, b, c, d} set => b
- SELECT MIN_DISTANCE(set, b) FROM {e, f, g} set => e or f or g
- SELECT MIN_DISTANCE(set, b) FROM {} set => no result

Answer

I think you just want a case expression:

SELECT (case when MIN_DISTANCE(set, b) in ('a', 'b', 'c', 'd') then 'b'
             when MIN_DISTANCE(set, b) in ('e', 'f', 'g') MIN_DISTANCE(set, b)
        end)
Comments