考虑使用以下代码。类型
enum_buysell
只包括两个值:buy
和sell
。有些情况下我需要获得相反的值,但是我认为代码看起来很丑。有没有一种优化的方法?我考虑过不使用枚举,改用布尔类型,但这个想法并不完美,因为它会使数据本身不太明显。select
datetime,
case
when account_id_active = p_account_id and direction = 'buy' then 'buy'::enum_buysell
when account_id_active = p_account_id and direction = 'sell' then 'sell'::enum_buysell
when account_id_passive = p_account_id and direction = 'buy' then 'sell'::enum_buysell
when account_id_passive = p_account_id and direction = 'sell' then 'buy'::enum_buysell
end as direction,
price,
volume
from
deals
where
account_id_active = p_account_id or
account_id_passive = p_account_id
order by
datetime desc
limit
10;