Since there is no function to get the next enumeration value in PostgreSQL, you have to define it yourself.
create function next_buysell (e enum_buysell) returns enum_buysell as $$ begin return (case when e='buy'::enum_buysell then 'sell'::enum_buysell else 'buy'::enum_buysell end); end $$ language plpgsql;
Now you can use it as follows:
postgres=# select next_buysell('sell'::enum_buysell); next_buysell -------------- buy (1 row) postgres=# select next_buysell('buy'::enum_buysell); next_buysell -------------- sell (1 row)
And your CASE statement will look like this:
case when account_id_active = p_account_id then direction when account_id_passive = p_account_id then next_buysell(direction) end as direction
source share