How to calculate Cummulative data
rosy91Apr 17 2012 — edited Apr 17 2012Hi
I need output like this.
Frequency Percent Cumulative Frequency Cumulative percent
4468 0.91 4468 0.91
21092 4.31 25560 5.23
57818 11.82 83378 17.05
I am using Oracle 9i.
My output data like this and I need to write the query for 3 columns (Frequency,Percent ,Cumulative frequency and Cumulative percent)
1:The formula for Frequency column data is sum of (dd+cc+mc_cc_mc).
1:The formula for Percent column data is (Frequency/Sum of cumulative frequency)*100
2:The formula for Cumulative Frequency column data is (Cumulative of Frequency column data)
3:The formula for Cumulative Percent column data is (Cumulative of Percent column data)
What should be the analytic function and how to write the query.Please find the sample data and table script.
CREATE TABLE all_lony (
campno varchar2(20),
dd INTEGER,
cc INTEGER,
mc INTEGER,
cc_mc INTEGER
);
insert into all_lony (campno,dd,cc,mc,cc_mc)
values(36,156,1320,445,2547);
insert into all_lony (campno,dd,cc,mc,cc_mc)
values(40,233,19711,263,885);
=============
Please find my query below
SELECT campno
|| ','
|| dm
|| ','
|| cc
|| ','
|| mc
|| ','
|| cc_mc
|| ','
|| frequency
|| ','
|| per
||','
||cumulative_fr
||','
|| SUM (per) OVER (ORDER BY per ROWS UNBOUNDED PRECEDING)
FROM (SELECT q3.campno campno, q3.dm, q3.cc, q3.mc, q3.cc_mc,
q3.frequency, q3.cumulative_fr,
(q3.Frequency / SUM (q3.cumulative_fr)) * 100 per
FROM (SELECT q2.campno campno, SUM (q2.dm) dm, SUM (q2.cc) cc,
SUM (q2.mc) mc, SUM (q2.cc_mc) cc_mc,
(SUM ( NVL (q2.dm, 0)
+ NVL (q2.cc, 0)
+ NVL (q2.mc, 0)
+ NVL (q2.cc_mc, 0)
)
) frequency,
SUM (SUM ( NVL (q2.dm, 0)
+ NVL (q2.cc, 0)
+ NVL (q2.mc, 0)
+ NVL (q2.cc_mc, 0)
)
) OVER (ORDER BY SUM ( NVL (q2.dm, 0)
+ NVL (q2.cc, 0)
+ NVL (q2.mc, 0)
+ NVL (q2.cc_mc,0)
) ROWS UNBOUNDED PRECEDING)
cumulative_fr
from all_lony
q1 )q2
GROUP BY q3.campno) q3
GROUP BY campno, dm, cc, mc,cc_mc, frequency,cumulative_fr)
Just verify the query and let me know