I'm trying to understand if I'm properly nesting these case expressions correctly. Is this the optimal way to go about nesting these?
To explain what I'm trying to do here is create a function that looks at three columns in a single table. A string result is determined based on the found input. If the input is not found in column 1 (aka test), then it moves onto column 2 (transpose) and then to column 3 (construction) until either a result is found or it moves on.
I'm unsure this is the best and or correct way to go about this situation. Any suggestions would be greatly appreciated.
CREATE OR REPLACE FUNCTION any_class(test TEXT, transpose TEXT, construction TEXT) RETURNS TEXT AS $$
SELECT CASE
WHEN test IN ('a', 'b') THEN 'result1'
WHEN test IN ('c', 'd') THEN 'result2'
WHEN test IN ('e', 'f') THEN 'result3'
WHEN test IN ('g', 'h') THEN 'result4'
WHEN test IN ('i', 'j') THEN 'result5'
WHEN test IN ('k', 'l', 'm') THEN 'result6'
WHEN test IN ('n') THEN 'result7'
WHEN test IN ('o') THEN 'result8'
WHEN test IN ('p') THEN 'result9'
WHEN test IN ('q', 'r', 's', 't', 'u') THEN 'result10'
ELSE CASE
WHEN transpose IN ('a', 'b') THEN 'result1'
WHEN transpose IN ('c', 'd') THEN 'result2'
WHEN transpose IN ('e', 'f') THEN 'result3'
WHEN transpose IN ('g', 'h') THEN 'result4'
WHEN transpose IN ('i', 'j') THEN 'result5'
WHEN transpose IN ('k', 'l', 'm') THEN 'result6'
WHEN transpose IN ('n') THEN 'result7'
WHEN transpose IN ('o') THEN 'result8'
WHEN transpose IN ('p') THEN 'result9'
WHEN transpose IN ('q', 'r', 's', 't', 'u') THEN 'result10'
ELSE CASE
WHEN construction IN ('a', 'b') THEN 'result1'
WHEN construction IN ('c', 'd') THEN 'result2'
WHEN construction IN ('e', 'f') THEN 'result3'
WHEN construction IN ('g', 'h') THEN 'result4'
WHEN construction IN ('i', 'j') THEN 'result5'
WHEN construction IN ('k', 'l', 'm') THEN 'result6'
WHEN construction IN ('n') THEN 'result7'
WHEN construction IN ('o') THEN 'result8'
WHEN construction IN ('p') THEN 'result9'
WHEN construction IN ('q', 'r', 's', 't', 'u') THEN 'result10'
END
END
END;
$$
LANGUAGE SQL
IMMUTABLE PARALLEL SAFE;