Search This Blog

Tuesday, 11 October 2011

Need to calculate 12 months aggregated data for a given date.


SOURCE
DEPARTMENT_NAMEANALYST_OFFICEANALYST_REGIONMONTH_YEARSUM(CNT)
Corporate RatingsHong KongAsia Pacific01/01/2009 00:00:00130
Corporate RatingsHong KongAsia Pacific02/01/2009 00:00:00107
Corporate RatingsHong KongAsia Pacific03/01/2009 00:00:00170
Corporate RatingsHong KongAsia Pacific04/01/2009 00:00:00237
Corporate RatingsHong KongAsia Pacific05/01/2009 00:00:00131
Corporate RatingsHong KongAsia Pacific06/01/2009 00:00:00213
Corporate RatingsHong KongAsia Pacific07/01/2009 00:00:0099
Corporate RatingsHong KongAsia Pacific08/01/2009 00:00:0081
Corporate RatingsHong KongAsia Pacific09/01/2009 00:00:00269
Corporate RatingsHong KongAsia Pacific10/01/2009 00:00:00326
Corporate RatingsHong KongAsia Pacific11/01/2009 00:00:00243
Corporate RatingsHong KongAsia Pacific12/01/2009 00:00:00195
Corporate RatingsHong KongAsia Pacific01/01/2010 00:00:0024
Corporate RatingsHong KongAsia Pacific02/01/2010 00:00:0060
Corporate RatingsHong KongAsia Pacific03/01/2010 00:00:0089
Corporate RatingsHong KongAsia Pacific04/01/2010 00:00:0095
Corporate RatingsHong KongAsia Pacific05/01/2010 00:00:0084
Corporate RatingsHong KongAsia Pacific06/01/2010 00:00:0064
Corporate RatingsHong KongAsia Pacific07/01/2010 00:00:0058
Corporate RatingsHong KongAsia Pacific08/01/2010 00:00:00186
Corporate RatingsHong KongAsia Pacific09/01/2010 00:00:0051
Corporate RatingsHong KongAsia Pacific10/01/2010 00:00:0058
Corporate RatingsHong KongAsia Pacific11/01/2010 00:00:00476
Corporate RatingsHong KongAsia Pacific12/01/2010 00:00:00305
Corporate RatingsHong KongAsia Pacific01/01/2011 00:00:0029
Corporate RatingsHong KongAsia Pacific02/01/2011 00:00:0040
Corporate RatingsHong KongAsia Pacific03/01/2011 00:00:0087
Corporate RatingsHong KongAsia Pacific04/01/2011 00:00:00192
Corporate RatingsHong KongAsia Pacific05/01/2011 00:00:0014
Corporate RatingsHong KongAsia Pacific06/01/2011 00:00:005
Corporate RatingsHong KongAsia Pacific08/01/2011 00:00:0023



TARGET
DEPARTMENT_NAMEANALYST_OFFICEANALYST_REGIONMONTH_YEARSUM(CNT)SUM(ROLLING_12_MONTHS_CNT)
Corporate RatingsHong KongAsia Pacific01/01/2009 00:00:00130130
Corporate RatingsHong KongAsia Pacific02/01/2009 00:00:00107237
Corporate RatingsHong KongAsia Pacific03/01/2009 00:00:00170407
Corporate RatingsHong KongAsia Pacific04/01/2009 00:00:00237644
Corporate RatingsHong KongAsia Pacific05/01/2009 00:00:00131775
Corporate RatingsHong KongAsia Pacific06/01/2009 00:00:00213988
Corporate RatingsHong KongAsia Pacific07/01/2009 00:00:00991,087
Corporate RatingsHong KongAsia Pacific08/01/2009 00:00:00811,168
Corporate RatingsHong KongAsia Pacific09/01/2009 00:00:002691,437
Corporate RatingsHong KongAsia Pacific10/01/2009 00:00:003261,763
Corporate RatingsHong KongAsia Pacific11/01/2009 00:00:002432,006
Corporate RatingsHong KongAsia Pacific12/01/2009 00:00:001952,201
Corporate RatingsHong KongAsia Pacific01/01/2010 00:00:00242,095
Corporate RatingsHong KongAsia Pacific02/01/2010 00:00:00602,048
Corporate RatingsHong KongAsia Pacific03/01/2010 00:00:00891,967
Corporate RatingsHong KongAsia Pacific04/01/2010 00:00:00951,825
Corporate RatingsHong KongAsia Pacific05/01/2010 00:00:00841,778
Corporate RatingsHong KongAsia Pacific06/01/2010 00:00:00641,629
Corporate RatingsHong KongAsia Pacific07/01/2010 00:00:00581,588
Corporate RatingsHong KongAsia Pacific08/01/2010 00:00:001861,693
Corporate RatingsHong KongAsia Pacific09/01/2010 00:00:00511,475
Corporate RatingsHong KongAsia Pacific10/01/2010 00:00:00581,207
Corporate RatingsHong KongAsia Pacific11/01/2010 00:00:004761,440
Corporate RatingsHong KongAsia Pacific12/01/2010 00:00:003051,550
Corporate RatingsHong KongAsia Pacific01/01/2011 00:00:00291,555
Corporate RatingsHong KongAsia Pacific02/01/2011 00:00:00401,535
Corporate RatingsHong KongAsia Pacific03/01/2011 00:00:00871,533
Corporate RatingsHong KongAsia Pacific04/01/2011 00:00:001921,630
Corporate RatingsHong KongAsia Pacific05/01/2011 00:00:00141,560
Corporate RatingsHong KongAsia Pacific06/01/2011 00:00:0051,501
Corporate RatingsHong KongAsia Pacific08/01/2011 00:00:00231,466


Solution:

select ETR_RA_AGG_FACT_KEY,department_name,ANALYST_OFFICE,ANALYST_REGION,month_year,sum(cnt),sum(ROLLING_12_MONTHS_CNT)
from (
select ETR_RA_AGG_FACT_KEY,DEPARTMENT_NAME,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION,sum(NO_OF_RATING_ACTIONS) as cnt,
sum(NO_OF_RATING_ACTIONS)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),1) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
 nvl(lag(sum(NO_OF_RATING_ACTIONS),2) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),3) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),4) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),5) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),6) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),7) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),8) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),9) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),10) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)+
nvl(lag(sum(NO_OF_RATING_ACTIONS),11) over (partition by department_name,ANALYST_OFFICE,ANALYST_REGION order by department_name,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION ),0)
as Rolling_12_months_cnt  from ETR_RATING_actions_AGG_FACT
group by ETR_RA_AGG_FACT_KEY,DEPARTMENT_NAME,MONTH_YEAR,ANALYST_OFFICE,ANALYST_REGION
)
where month_year between to_date('01/01/2009','dd/mm/yyyy') AND TRUNC(SYSDATE)
group by ETR_RA_AGG_FACT_KEY,department_name,month_year,ANALYST_OFFICE,ANALYST_REGION

No comments:

Post a Comment