1

I have a table that has rows that look like this

Account Currency Amount
1 USD 5
1 EUR 10
2 USD 8
3 EUR 4

Is there a way to do a select query so it would return something like

Account amount_usd amount_eur
1 5 10
2 8
3 4

usd and eur are hardcoded, so i dont have to build the name from the column.

2

2 Answers 2

1

You can use conditional aggregation which in Postgres uses filter:

select account,
       sum(amount) filter (where currency = 'USD') as usd_amount,
       sum(amount) filter (where currency = 'EUR') as eur_amount
from t
group by account;
Sign up to request clarification or add additional context in comments.

Comments

0

You can use sum() with case when condition :

select account, sum(case when currency='USD' then amount end) Amount_USD,
sum(case when currency='EUR' then amount end) Amount_EUR
from myTable
Group by account

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.