[英]Want to calculate cumulative percentage from one table to another table multiple columns
輸入
Table 1 Table 2
Amount 1 2 3 4 5
100 10 10 10 10 10
200 20 20 20 20 20
Output
1 2 3 4 5
10% 20% 30% 40% 50%
10% 20% 30% 40% 50%
我嘗試在 EXCEL 中執行此操作並成功,但現在數據太大,無法在 excel 中執行此操作
不適用
\
我假設兩個表中都有一些列可用於連接表(沒有這個,您的問題將無法解決,除非每個表中只有一條記錄)。
所以假設以下表結構:
table1
id
amount
table2
id
col1
col2
col3
col4
col5
您可以連接兩個表並按如下方式進行計算:
select
t2.col1/t1.amount as col1,
(t2.col1 + t2.col2)/t1.amount as col2,
(t2.col1 + t2.col2 + t2.col3)/t1.amount as col3,
(t2.col1 + t2.col2 + t2.col3 + t2.col4)/t1.amount as col4,
(t2.col1 + t2.col2 + t2.col3 + t2.col4 + t2.col5)/t1.amount as col5
from table1 t1
inner join table2 t2 on t2.id = t1.id
結果集中的每一列(除了id
)將包含一個介於 0 和 1 之間的數值,它表示amount
的累積部分。 然后,您可以處理應用程序中的百分比格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.