By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table your_table (ID, PERIOD_1, PERIOD_2, PERIOD_3) as
select 0, 6, 5, 5 from dual union all
select 1, 120, 124, 85 from dual union all
select 2, 183, 159, 99 from dual union all
select 3, 60, 42, 27 from dual union all
select 4, 24, 19, 8 from dual union all
select 5, 1, null, null from dual
6 rows affected
select id, period_1, period_2, period_3,
nvl(period_1, 0) + nvl(period_2, 0) + nvl(period_3, 0) as total,
round(100 * period_1 / (nvl(period_1, 0) + nvl(period_2, 0) + nvl(period_3, 0)), 2) as period_1_perc,
round(100 * period_2 / (nvl(period_1, 0) + nvl(period_2, 0) + nvl(period_3, 0)), 2) as period_2_perc,
round(100 * period_3 / (nvl(period_1, 0) + nvl(period_2, 0) + nvl(period_3, 0)), 2) as period_3_perc
from your_table;
ID | PERIOD_1 | PERIOD_2 | PERIOD_3 | TOTAL | PERIOD_1_PERC | PERIOD_2_PERC | PERIOD_3_PERC |
---|---|---|---|---|---|---|---|
0 | 6 | 5 | 5 | 16 | 37.5 | 31.25 | 31.25 |
1 | 120 | 124 | 85 | 329 | 36.47 | 37.69 | 25.84 |
2 | 183 | 159 | 99 | 441 | 41.5 | 36.05 | 22.45 |
3 | 60 | 42 | 27 | 129 | 46.51 | 32.56 | 20.93 |
4 | 24 | 19 | 8 | 51 | 47.06 | 37.25 | 15.69 |
5 | 1 | null | null | 1 | 100 | null | null |
select id, period_1, period_2, period_3,
round(100 * period_1 / total, 2) as period_1_perc,
round(100 * period_2 / total, 2) as period_2_perc,
round(100 * period_3 / total, 2) as period_3_perc
from (
select id, period_1, period_2, period_3,
nvl(period_1, 0) + nvl(period_2, 0) + nvl(period_3, 0) as total
from your_table
);
ID | PERIOD_1 | PERIOD_2 | PERIOD_3 | PERIOD_1_PERC | PERIOD_2_PERC | PERIOD_3_PERC |
---|---|---|---|---|---|---|
0 | 6 | 5 | 5 | 37.5 | 31.25 | 31.25 |
1 | 120 | 124 | 85 | 36.47 | 37.69 | 25.84 |
2 | 183 | 159 | 99 | 41.5 | 36.05 | 22.45 |
3 | 60 | 42 | 27 | 46.51 | 32.56 | 20.93 |
4 | 24 | 19 | 8 | 47.06 | 37.25 | 15.69 |
5 | 1 | null | null | 100 | null | null |