sql - calculate moving avg in Oracle and show the periods -


i'm trying calculate 10 period moving avg in oracle sql can't seem right.

my query follows:

select           bsm_id           ,case                 when d_dtm = (select max(d_dtm)-10/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period1"           ,case                 when d_dtm = (select max(d_dtm)-9/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period2"           ,case                 when d_dtm = (select max(d_dtm)-8/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period3"           ,case                 when d_dtm = (select max(d_dtm)-7/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period4"                      ,case                 when d_dtm = (select max(d_dtm)-6/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period5"                      ,case                 when d_dtm = (select max(d_dtm)-5/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period6"                       ,case                 when d_dtm = (select max(d_dtm)-4/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period7"                       ,case                 when d_dtm = (select max(d_dtm)-3/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period8"               ,case                 when d_dtm = (select max(d_dtm)-2/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period9"                       ,case                 when d_dtm = (select max(d_dtm)-1/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end "period10"                       ,avg(                 case                 when d_dtm between (select max(d_dtm)-1/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi) , (select max(d_dtm)-10/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)                  (case when sum(v_att_cnt) = 0 null else sum(v_cust_blk_cnt)/sum(v_att_cnt) end)                  end               ) "10 period avg"       dmsn.ds3r_fh_1xrtt_bts_lvl_kpi     d_dtm >= (select max(d_dtm)-10/24 dmsn.ds3r_fh_1xrtt_bts_lvl_kpi)       group           bsm_id     order bsm_id desc 

but keep getting "not single-group group function" error. i'm trying show % each of 10 periods columns, , have 11th column avg. remember doing similar in sql can't syntanx right here in plsql

in sql server able achieve same result:

  select         carrier,              division,              location,              servicetype              ,max(@maxwk-1) ops_week_id              ,max(case when ops_week_id = @maxwk-2 (ot * 100.00) / ts else null end)                [%_wk_1 ]               ,max(case when ops_week_id = @maxwk-3 (ot * 100.00) / ts else null end)                [%_wk_2 ]                 ,max(case when ops_week_id = @maxwk-4 (ot * 100.00) / ts else null end)                [%_wk_3 ]              ,max(case when ops_week_id = @maxwk-5 (ot * 100.00) / ts else null end)                [%_wk_4 ]              ,avg(case when ops_week_id between @maxwk-5 , @maxwk - 2  (ot * 100.00) / ts else null end)                [4_wk_sma]              ,avg(case when ops_week_id between @maxwk-5 , @maxwk - 2  (ot * 100.00) / ts else null end)                - 0.15 [lwrband]                          ,avg(case when ops_week_id between @maxwk-5 , @maxwk - 2  (ot * 100.00) / ts else null end)                + 0.15 [uprband]                ,max(case when ops_week_id = @maxwk-1 (ot * 100.00) / ts else null end)                [ops_wk_id_av]           la         group la.division,la.location,la.servicetype,la.carrier 

the original table structure looks such:

     d_dtm (datetime)      f_id      reg_dtm      mrkt_id      mrkt_nm      cl_id      cl_nm      bsm_id      bsm_nm      bsc_seq_id      cscd_id      cscd_nm      bts_id      v_att_cnt (denominator)      v_mbl_org_cnt      v_mbl_ter_cnt      v_silent_retry_cnt      v_cust_blk_cnt (numerator) 

what want take percentage (v_cust_blk_cnt/v_att_cnt) last 10 hours, , avg them bsm_id. resulting query have structure such

bsm_id | period1% | period2% | period3% | etc... | avg%

i feel should able use case whens can't seem group , syntax work...

you getting error because grouping bsm_id, referencing many other columns without aggregate functions.

if using oracle, use built-in functions moving average. these analytic functions range keyword, like:

select bsm.*,        (sum(v_cust_blk_cnt) on (partition bsm_id order d_dtm range between 10 preceding , current row) /        (sum(v_cust_att_cnt) on (partition bsm_id order d_dtm range between 10 preceding , current row)) bsm 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -