cust_id acct_id trxn_amt trxn_cnt
1 66685638 10,028,717,398 199.75 5
2 66685638 10,028,849,377 76.16 2假设我有一个具有多个帐户ID的客户表,我想为每个客户创建一个新列(对于cust_id=66685638,为199.75+76.16),它是每个客户所有交易金额的总和,以及另一个列,即每个帐户总支出的百分比(对于第一个帐户,为199.75/(76.15+199.75))。每个客户可能有2-4个acct_ids。
非常感谢。
发布于 2013-06-27 04:27:57
下面是什么:
select cust_id,
sum(trxn_amt) as total_amount,
trxn_amt / sum(trxn_amt) as pct
from customers
group by cust_id
order by cust_id;或者,如果您想要查看customers表中的每一行:
select cust_id,
acct_id,
trxn_amt,
sum(trxn_amt) as over (partition by cust_id) as total_amount,
trxn_amt / sum(trxn_amt) as over (partition by cust_id) as pct
from customers
order by cust_id;https://stackoverflow.com/questions/17329248
复制相似问题