简体   繁体   中英

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

    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

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 . You can then take care of the percent formatting in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM