-- 잘못이해했네요.. 수정. WITH T ( NM , RT ) as ( SELECT 'A' , 33.33 FROM DUAL UNION ALL SELECT 'B' , 33.33 FROM DUAL UNION ALL SELECT 'C' , 33.34 FROM DUAL ) SELECT NM , RT , AMT , CASE WHEN RN = 1 THEN AMT + 11 - SUM(AMT) OVER() ELSE AMT END AMT2 FROM ( SELECT NM , RT , ROUND(( RT / 100 ) * 11 , 2 ) AS AMT , ROW_NUMBER() OVER(ORDER BY NM DESC) RN FROM T ) ORDER BY NM
/* 총금액(AMT), 분배수(DIV)를 입력받아, 총금액/분배수를 소수점2자리 반올림처리한 후, 그 합계금액과 최초총금액의 차이를 임의의 대상(행)에서 보정처리 합니다. 2019.03.08, Jun H. Lee */ SELECT AMT /*총금액*/ , SEQ /*일련번호*/ , OBJ /*대상*/ , DIV_AMT /*분배금액*/ , RAN_SEQ /*임의일련번호*/ , DIV_AMT +DECODE(RAN_SEQ, 1, AMT-SUM(DIV_AMT)OVER(), 0) AS MOD_DIV_AMT /*수정분배금액*/ FROM ( SELECT AMT /*총금액*/ , LEVEL AS SEQ /*일련번호*/ , CHR(64+LEVEL) AS OBJ /*대상*/ , ROUND(AMT/DIV, 2) AS DIV_AMT /*분배금액*/ , ROW_NUMBER()OVER(ORDER BY DBMS_RANDOM.RANDOM) AS RAN_SEQ /*임의일련번호*/ FROM ( SELECT 11 AS AMT /*총금액*/ /*입력변수01*/ , 3 AS DIV /*분배수*/ /*입력변수02*/ FROM DUAL ) CONNECT BY LEVEL <= DIV ) ORDER BY SEQ ;