WITH T AS ( select 'ABC' AS FLAG1,'A' AS FLAG2,'35' AS CNT,'A01' AS line from dual union all select 'ABC' AS C0,'B' AS C1,'44' AS C2,'A02' AS C3 from dual union all select 'ABC' AS C0,'C' AS C1,'46' AS C2,'A03' AS C3 from dual union all select 'ABC' AS C0,'D' AS C1,'29' AS C2,'A04' AS C3 from dual union all select 'ABC' AS C0,'E' AS C1,'20' AS C2,'A05' AS C3 from dual ) SELECT FLAG1 , FLAG2 , CNT , ROUND(CNT / SUM(CNT) OVER () * 100) AVG , LINE FROM T
하고 보니 avg가 total 100 이 안 되네요?? 잠만 생각을.. ㅋ
WITH T AS ( select 'ABC' AS FLAG1,'A' AS FLAG2,'35' AS CNT,'A01' AS line from dual union all select 'ABC' AS C0,'B' AS C1,'44' AS C2,'A02' AS C3 from dual union all select 'ABC' AS C0,'C' AS C1,'46' AS C2,'A03' AS C3 from dual union all select 'ABC' AS C0,'D' AS C1,'29' AS C2,'A04' AS C3 from dual union all select 'ABC' AS C0,'E' AS C1,'20' AS C2,'A05' AS C3 from dual ) SELECT FLAG1, FLAG2, CNT, ROUND(RATIO_TO_REPORT("CNT") OVER() * 100, 2) "AVG", LINE FROM T; Reference : http://20140501.tistory.com/197
SELECT FLAG1, FLAG2, CNT , DECODE(TOTROW, RN, 100 - SUM(AVG) OVER(ORDER BY RN ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), AVG) AVG , LINE FROM ( SELECT FLAG1 , FLAG2 , CNT , ROUND(CNT / SUM(CNT) OVER () * 100) AVG , LINE , ROWNUM RN , COUNT(1) OVER() TOTROW FROM T )
뭔가 더 좋은 방법이 있을 것 같은데 모르겠네요. 다시 일하러 갑니다.