oracle 비율로 배분 후 나머지 마지막에 합산
select b.key_cd,
b.key_seq,
b.key_qty,
b.aloc_qty,
b.last_seq,
ratio_to_report(b.key_qty) over() * 100 as rate,
decode(key_seq, last_seq, 1000 - sum(aloc_qty) over() + aloc_qty, aloc_qty) as rtn_qty
from (
select a.key_cd,
row_number() over(order by key_cd) as key_seq,
a.key_qty,
floor(1000 * key_qty / sum(key_qty) over()) as aloc_qty,
count(*) over() as last_seq
from (
select 'A' as key_cd, 11 as key_qty from dual
union all
select 'B' as key_cd, 60 as key_qty from dual
union all
select 'C' as key_cd, 34 as key_qty from dual
union all
select 'D' as key_cd, 77 as key_qty from dual
union all
select 'E' as key_cd, 51 as key_qty from dual
union all
select 'F' as key_cd, 22 as key_qty from dual
) a
) b
A 1 11 43 6 4.31372549 43
B 2 60 235 6 23.52941176 235
C 3 34 133 6 13.33333333 133
D 4 77 301 6 30.19607843 301
E 5 51 200 6 20 200
F 6 22 86 6 8.62745098 88
'Oracle(DB관련)' 카테고리의 다른 글
oracle text (0) | 2020.04.20 |
---|---|
oracle java source (0) | 2020.04.02 |
oracle alert log 파일 조회 diy$alert (0) | 2019.11.08 |
oracle string util package (0) | 2018.12.28 |
oracle audit (0) | 2018.11.22 |