Sql 如何使用select合并postgres中的两行

Sql 如何使用select合并postgres中的两行,sql,postgresql,Sql,Postgresql,我有一张桌子,上面有这样的行 账户 通货 数量 1. 美元 5. 1. 欧元 10 2. 美元 8. 3. 欧元 4. 您可以使用条件聚合,它在Postgres中使用过滤器: select account, sum(amount) filter (where currency = 'USD') as usd_amount, sum(amount) filter (where currency = 'EUR') as eur_amount from t group by

我有一张桌子,上面有这样的行

账户 通货 数量 1. 美元 5. 1. 欧元 10 2. 美元 8. 3. 欧元 4.
您可以使用条件聚合,它在Postgres中使用
过滤器

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;
在以下情况下,可以将sum()与case一起使用:

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

这个问题很好地解释了这一点:这是否回答了你的问题?