[英]Want to calculate cumulative percentage from one table to another table multiple columns
Input输入
Table 1 Table 2
Amount 1 2 3 4 5
100 10 10 10 10 10
200 20 20 20 20 20
Output Output
1 2 3 4 5
10% 20% 30% 40% 50%
10% 20% 30% 40% 50%
I tried to do it in EXCEL and succeed but the data is too large now to do it in excel我尝试在 EXCEL 中执行此操作并成功,但现在数据太大,无法在 excel 中执行此操作
NA不适用
\ \
I assume that there is some column in both tables that can be used to join the tables (without this, your question cannot be solved, unless there is only one record in each table).我假设两个表中都有一些列可用于连接表(没有这个,您的问题将无法解决,除非每个表中只有一条记录)。
So assuming the following table structures:所以假设以下表结构:
table1
id
amount
table2
id
col1
col2
col3
col4
col5
You can join both tables and do the computation as follows:您可以连接两个表并按如下方式进行计算:
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
Each column in the result set (apart from id
) will contain a numeric value between 0 and 1 that represents the cumulative portion of the amount
.结果集中的每一列(除了
id
)将包含一个介于 0 和 1 之间的数值,它表示amount
的累积部分。 You can then take care of the percent formatting in your application.然后,您可以处理应用程序中的百分比格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.