如何在 SQL Teradata 中根据金额和货币进行聚合?

How to do aggregation based on amount and currency in SQL Teradata?

我在 SQL Teradata 中有 table,如下所示:

client|currency|amount
------|--------|-----
1233  |CZK     |300
1233  |CZK     |100
4577  |EUR     |200
1233  |EUR     |500

我想计算每个客户在每种货币下的总金额和平均金额以及使用每种货币为客户进行的交易数量。

所以使用上面的 table 我需要得到如下结果:

client|avg_CZK |sum_CZK | avg_EUR | sum_EUR|number_CZ|number_EUR|
------|--------|--------|---------|--------|---------|----------
1233  |200     |400     | 500     |500     |2        |1
4577  |0       |0       |200      |200     |0        |1

我如何在 SQL Teradata 中做到这一点?

只使用条件聚合:

select client,
       avg(case when currency = 'CZK' then amount end) as avg_czk,
       sum(case when currency = 'CZK' then amount end) as sum_czk,
       avg(case when currency = 'EUR' then amount end) as avg_eur,
       sum(case when currency = 'EUR' then amount end) as sum_eur,
       sum(case when currency = 'CZK' then 1 else 0 end) as num_czk,
       sum(case when currency = 'EUR' then 1 else 0 end) as num_eur
from t
group by client;