I am attempting to get a value from a table based on “Preference”.
I have a table “t1” including two columns “id” and “alphabet”
Sample data
1 A
1 B
2 A
3 A
3 B
It means every id has the default value “A” and some have “B”).
Now I would like to get the values of “id” based on preference of “B”. Thus, if an ID has both A and B, I want the B row. If no B row is found, then the default A should be returned.
This might be slightly off as mysql dialect always throws me. Since B falls after A in an alphabetical sort, if you group on the ID and then select the maximum, it will give you all the B rows where they exist, otherwise you will get the A row.
Now, if your data is more complex than what you’ve indicated, you will need to examine whether the MAX logic still applies.