0
    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_id。

非常感谢。

4

1 回答 1

2

关于什么:

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;

或者,如果您想查看客户表中的每一行:

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;
于 2013-06-26T20:27:56.510 回答