如何在Oracle中导出加权平均值时汇总值?

时间:2019-05-14 11:17:19

标签: sql database oracle

我需要得出一个消费百分比,其逻辑基于加权平均值。下面是我正在运行的代码。     我试图找出不同聚合级别的Consensus_Plan_Consumption_pc字段的值。可以按周,按月,按年等。但是,如果运行以下代码,则基本上可以看到1年的12个值,这是由于inp.t_ep_fiscal_month_id字段为我提供了财政月份。我想知道如何编写代码,以便在用户要求以月,年或季度为单位时,它本身可以聚合。

SELECT item1.item,
    fiscal_year,inp.t_ep_fiscal_month_id , curr_fscl_month.t_ep_fiscal_month_id,
case when inp.t_ep_fiscal_month_id < curr_fscl_month.t_ep_fiscal_month_id 
        then 
        (sum(nvl(sales.actual_quantity, 0) *1 ) / sum( sales.xxdm_dp_plan_pm *1))
        else 
        ( 
        case when 
        (sum(case when from_unixtime(unix_timestamp(sales.sales_date), 'yyyyMMdd') <curr_fscl_month.pval then nvl(sales.actual_quantity, 0)
        else
        DECODE(NVL(sales.xxdm_cons_flag,1)
        ,1,NVL(sales.wcp_override, 0) + NVL(sales.xxdm_ref_dp_override, 0) + NVL( sales.xxdm_asia_dp_override , 0 )
        ,NVL(sales.xxdm_combined_fcst,0))
        end )) > 0 
        then (sum(nvl(sales.actual_quantity,0) + nvl(xxdm_open_orders,0))) / ((sum(case when from_unixtime(unix_timestamp(sales.sales_date), 'yyyyMMdd') <curr_fscl_month.pval then nvl(sales.actual_quantity, 0)
        else
        DECODE(NVL(sales.xxdm_cons_flag,1)
        ,1,NVL(sales.wcp_override, 0) + NVL(sales.xxdm_ref_dp_override, 0) + NVL( sales.xxdm_asia_dp_override , 0 )
        ,NVL(sales.xxdm_combined_fcst,0))
        end )))
        end
        ) Consensus_Plan_Consumption_pc
FROM prod_corp_frank.sales_data sales
LEFT OUTER JOIN prod_comres_sc_core.xxdm_dm_comb_v app ON (sales.item_id = app.item_id AND sales.location_id = app.location_id)
LEFT OUTER JOIN
  (
  SELECT inp.datet,
          fs_month.T_EP_FISCAL_MONTH_CODE fiscal_month,
          fs_week.T_EP_FISCAL_WEEK_CODE fiscal_week,
          fs_quarter.T_EP_FISCAL_quarter_CODE fiscal_quarter,
          inp.t_ep_fiscal_year_id fiscal_year,
          inp.t_ep_fiscal_month_id
   FROM prod_corp_frank.inputs inp
   LEFT OUTER JOIN prod_corp_frank.t_ep_fiscal_month fs_month ON (inp.t_ep_fiscal_month_id = fs_month.t_ep_fiscal_month_id)
   LEFT OUTER JOIN prod_corp_frank.t_ep_fiscal_week fs_week ON (inp.t_ep_fiscal_week_id = fs_week.t_ep_fiscal_week_id)
   LEFT OUTER JOIN prod_corp_frank.t_ep_fiscal_quarter fs_quarter ON (inp.t_ep_fiscal_quarter_id = fs_quarter.t_ep_fiscal_quarter_id)
   ) inp 
   ON (from_unixtime(unix_timestamp(sales.sales_date), 'yyyyMMdd') = from_unixtime(unix_timestamp(inp.datet), 'yyyyMMdd'))
LEFT OUTER JOIN prod_comres_sc_core.xxdm_dm_item_v item1 ON (sales.item_id = item1.item_id)
LEFT OUTER JOIN prod_corp_frank.t_ep_item tand1 on (item1.xxdm_tandem_item1=tand1.t_ep_item_ep_id)
LEFT OUTER JOIN prod_corp_frank.t_ep_item tand2 on (item1.xxdm_tandem_item2=tand2.t_ep_item_ep_id)
LEFT OUTER JOIN prod_corp_frank.t_ep_item tand3 on (item1.xxdm_tandem_item3=tand3.t_ep_item_ep_id)
LEFT OUTER JOIN prod_comres_sc_core.xxdm_dm_loc_v loc ON sales.location_id = loc.location_id,
(
select from_unixtime(unix_timestamp(sys.pval,'MM-dd-yyyy'), 'yyyyMMdd') pval,
inp1.t_ep_fiscal_month_id
from prod_corp_frank.SYS_PARAMS sys,
prod_corp_frank.inputs inp1
where pname='min_fore_sales_date'
and from_unixtime(unix_timestamp(sys.pval,'MM-dd-yyyy'), 'yyyyMMdd')
= from_unixtime(unix_timestamp(inp1.datet), 'yyyyMMdd')
) curr_fscl_month --calculate current fiscal_month
WHERE item1.item='ZP98'
and fiscal_year between 2016 and 2019
    GROUP BY item1.item,
    fiscal_year,
    inp.t_ep_fiscal_month_id,
    curr_fscl_month.t_ep_fiscal_month_id

0 个答案:

没有答案