我有一个表,其中的行看起来像这样
帐户 | 货币 | 数量 |
---|---|---|
1个 | 美元 | 5 |
1个 | 欧元 | 10 |
2个 | 美元 | 8 |
3 | 欧元 | 4 |
有没有一种方法可以执行选择查询,所以它会返回类似
帐户 | amount_usd | amount_eur |
---|---|---|
1个 | 5 | 10 |
2个 | 8 | |
3 | 4 |
usd和eur是硬编码的,因此我不必从列中构建名称。
您可以使用在Postgres中使用的条件聚合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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句