sum(
case
when currency = 'USD' then amount * exrt.rate
when currency = 'EUR' then amount * exrt.rate
when currency = 'GBP' then amount * exrt.rate
end )
as total_spend from transactions join (...) as exrt on transactions.currency=exrt.currency
group by user_id;
```
it looks like a working query, but i don't know if it's sane to do this kind of query or not? (the exrt) query is itself a long query that i have calculated using two selects and ...
Looks like the right way. And you don't need CASE here, only amount*rate. Be aware of transactions with currency not existing in exrt.
Обсуждают сегодня