Suppose I have the following events table:
Column | Type |
--------+---------+
id | integer |
type | integer |
The event type can be any user provided number and there is no relation on any other table in the system associated with this type.
The request came in that I generate a report and I need to match the type to a user provided string.
The user may provide the following:
1 = "Created event"
2 = "Deleted event"
3 = "Updated event"
This is overly simplified, but you get the idea.
Now I need to generate the report that includes the event table but instead of the type the matching string should be used.
I read the manual on the string functions and found the replace function which only solves the problem partially - I can only replace on type:
select replace(type::text, '3', 'Updated event') from events;
This will return all the events rows and only the rows that have the type 3 will have the correct string, the others will have the integer.
How can I leverage that to be able to match the type to a user string in one select?