2013-06-26 51 views
1
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爲199.75 + 76.16 = 66685638),另一列爲每個賬戶總支出的百分比(對於第一個賬戶爲199.75 /(76.15 + 199.75))。每個客戶可能擁有2-4個acct_ids。SQL-Teradata:如何創建一個由公式組成的新列

非常感謝。

+0

你的問題並不能說明問題的一小部分了解所解決。告訴我們你試圖去做什麼,爲什麼它沒有工作,以及它應該如何工作。另請參見:[堆棧溢出問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist) – Kermit

回答

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; 
相關問題