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;

または、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;
于 2013-06-26T20:27:56.510 に答える