Could someone please assist with the below query ?
I have a table like below :
Code alias user
-------- ----- -------
7305553 BPP (null)
8136852 BPP AYU
8136852 BPP TKL
7305553 BPFX (null)
8136848 BPFX YAO
11903927 CIX (null)
And I want to retrieve the "Code" value by passing "alias" and "user" values, however when the "user" doesn't match/exist, I'd need the row with the null user. Eg :
select Code from my_table where alias = 'BPP' and user = 'TEST'
should return the 1st line code value (7305553) , as user "TEST" doesn't exist. Is this kind of best-matching behavior possible with some kind of conditional "where" ? (with some kind of case statement?)
select Code from my_table where alias = 'BPP' and (user = 'TEST' OR user IS NULL) ORDER by user ASC;Then you get one or two rows. Just adjust the ORDER clause to get the NULL as a second record when you have two ones, so you are sure which one is the NULL.