aşağıdaki kodu kullanmayı düşünün. buy
ve sell
: Tip enum_buysell
sadece 2 değerlerini içerir. Bazı durumlarda sadece karşıt değeri almam gerekiyor, ama kod çirkin görünüyor, imho. Bunu optimize etmenin bir yolu var mı? Enum kullanmadığını düşündüm, örn. Bunun yerine boolean yapın, ancak bu fikir, verilerin kendisini daha az belirgin hale getirdiği için mükemmel değildir.Postgres: Nasıl bir enum sette sonraki öğeyi almak için?
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;
Mantık sorun: Bir enum * sırasız temelden * olduğunu. – wildplasser
@wildplasser temelden kesinlikle haklısın, ama bu, postgres'in doğru değil - var [dokümanlar bir göz: 8.7.2. , Sipariş] (http://www.postgresql.org/docs/9.4/static/datatype-enum.html) –