PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Cerca un modello in una determinata fascia di prezzo in valute diverse

Puoi utilizzare SQL CASE per questo:

SELECT price, currency,
       CASE WHEN currency='USD' THEN price * 1
            WHEN currency='RUB' THEN price * 65
            ELSE price
       END as final_price
FROM products
WHERE final_price BETWEEN 10 AND 100

Versione binari:

Product.select("price, currency, CASE 
  WHEN currency='USD' THEN price * 1  
  WHEN currency='RUB' THEN price * 65 
  ELSE price END as final_price")
.where("final_price BETWEEN ? AND ?", 10, 100)