无法理解分区而不是查询

时间:2014-07-15 11:01:47

标签: sql oracle

    /* Formatted on 2014/07/15 11:08 (Formatter Plus v4.8.5) */
       SELECT   si_dda_platform.platform, si_dda_rel_contr.ctolocpr,
       si_dda_prod.codprod, si_dda_prod.nomprod2,
       AVG (AVG (rpt_ft_dc_con_c_d.imporip))  
       OVER (PARTITION BY rpt_ft_dc_con_c_d.idgruemp, 
       rpt_ft_dc_con_c_d.fecdata, 
       rpt_ft_dc_con_c_d.codcont, 
       rpt_ft_dc_con_c_d.idempr, 
       rpt_ft_dc_con_c_d.idcent, 
       rpt_ft_dc_con_c_d.codprod, 
       rpt_ft_dc_con_c_d.idcontr, 
       rpt_ft_dc_con_c_d.idscontr)
     / SUM (1) OVER (PARTITION BY rpt_ft_dc_con_c_d.idgruemp,  -- sum over partition
     rpt_ft_dc_con_c_d.fecdata, 
     rpt_ft_dc_con_c_d.codcont, 
     rpt_ft_dc_con_c_d.idempr, 
     rpt_ft_dc_con_c_d.idcent, 
     rpt_ft_dc_con_c_d.codprod, 
     rpt_ft_dc_con_c_d.idcontr, 
     rpt_ft_dc_con_c_d.idscontr),
       AVG (AVG (rpt_ft_dc_con_c_d.imporico)) OVER ,  -- Avg over partition
       (PARTITION BY rpt_ft_dc_con_c_d.idgruemp, 
       rpt_ft_dc_con_c_d.fecdata, 
       rpt_ft_dc_con_c_d.codcont, 
       rpt_ft_dc_con_c_d.idempr, 
       rpt_ft_dc_con_c_d.idcent, 
       rpt_ft_dc_con_c_d.codprod, 
       rpt_ft_dc_con_c_d.idcontr, 
       rpt_ft_dc_con_c_d.idscontr)
     / SUM (1) OVER (PARTITION BY rpt_ft_dc_con_c_d.idgruemp, -- Sum over partition    
      rpt_ft_dc_con_c_d.fecdata, 
      rpt_ft_dc_con_c_d.codcont, 
      rpt_ft_dc_con_c_d.idempr, 
      rpt_ft_dc_con_c_d.idcent, 
      rpt_ft_dc_con_c_d.codprod, 
      rpt_ft_dc_con_c_d.idcontr, 
      rpt_ft_dc_con_c_d.idscontr),
     SUM (rpt_ft_dd_plbal.isafccdp)
   FROM si_dda_rel_contr,
     rpt_ft_dc_contr rpt_ft_dc_con_c_d,
     si_dda_prod,
     si_dda_scen,
     si_ddj_crit,
     si_dda_platform,
     rpt_ft_dd_plbal,
     rpt_cnf_fechas_d,
     rpt_cnf_period_d
     WHERE (    rpt_ft_dd_plbal.idempr = si_dda_prod.idempr
       AND rpt_ft_dd_plbal.codprod = si_dda_prod.codprod
     )
     AND (    rpt_ft_dd_plbal.idempr = si_ddj_crit.idempr
      AND rpt_ft_dd_plbal.codcrite = si_ddj_crit.codjen6
     )
    AND (    rpt_ft_dd_plbal.idempr = si_dda_scen.idempr
      AND rpt_ft_dd_plbal.idescena = si_dda_scen.idescena
     )
     AND (rpt_cnf_fechas_d.fecdata = rpt_ft_dd_plbal.fecdata)
     AND (rpt_ft_dd_plbal.idgruemp = '0022')
     AND (rpt_ft_dd_plbal.acumtemp = rpt_cnf_period_d.acumtemp)
      AND (    rpt_ft_dd_plbal.idgruemp = rpt_ft_dc_con_c_d.idgruemp(+)
      AND rpt_ft_dd_plbal.fecdata = rpt_ft_dc_con_c_d.fecdata(+)
      AND rpt_ft_dd_plbal.codcont = rpt_ft_dc_con_c_d.codcont(+)
      AND rpt_ft_dd_plbal.idempr = rpt_ft_dc_con_c_d.idempr(+)
      AND rpt_ft_dd_plbal.idcent = rpt_ft_dc_con_c_d.idcent(+)
      AND rpt_ft_dd_plbal.codprod = rpt_ft_dc_con_c_d.codprod(+)
      AND rpt_ft_dd_plbal.idcontr = rpt_ft_dc_con_c_d.idcontr(+)
      AND rpt_ft_dd_plbal.idscontr = rpt_ft_dc_con_c_d.idscontr(+)
     )
      AND (    rpt_ft_dd_plbal.idempr = si_dda_rel_contr.idempr(+)
      AND rpt_ft_dd_plbal.idcent = si_dda_rel_contr.idcent(+)
      AND rpt_ft_dd_plbal.codprod = si_dda_rel_contr.idprod(+)
      AND rpt_ft_dd_plbal.idcontr = si_dda_rel_contr.idcontr(+)
     )
      AND (    rpt_ft_dd_plbal.idempr = si_dda_platform.idempr
      AND rpt_ft_dd_plbal.codprod = si_dda_platform.codprod
      AND rpt_ft_dd_plbal.codsprod = si_dda_platform.codsprod
      AND rpt_ft_dd_plbal.coestref = si_dda_platform.coestref
     )
      AND (    to_char(rpt_cnf_fechas_d.fecdata,'YYYYMMDD') ='20140228'
      AND ((rpt_cnf_period_d.acumtemp) = 'DAY')
      AND ((si_dda_scen.idescena) = '01')
      AND ((si_ddj_crit.codjen3) = '30010')
     )
       AND si_dda_platform.platform = 'ALFA'
    GROUP BY 1,
     rpt_ft_dc_con_c_d.idgruemp,
     rpt_ft_dc_con_c_d.fecdata,
     rpt_ft_dc_con_c_d.codcont,
     rpt_ft_dc_con_c_d.idempr,
     rpt_ft_dc_con_c_d.idcent,
     rpt_ft_dc_con_c_d.codprod,
     rpt_ft_dc_con_c_d.idcontr,
     rpt_ft_dc_con_c_d.idscontr,
     si_dda_platform.platform,
     si_dda_rel_contr.ctolocpr,
     si_dda_prod.codprod,
     si_dda_prod.nomprod2

1 个答案:

答案 0 :(得分:0)

您可以以抽象的方式简化查询。 (我只是看了一下WHERE子句。)

SELECT   
    <column_list>
    AVG (<expression>) OVER (PARTITION BY <column_list>) / SUM (1) OVER (PARTITION BY <column_list>),
    AVG (<expression>) OVER (PARTITION BY <column_list>) / SUM (1) OVER (PARTITION BY <column_list>),
    SUM (<column_name>)
FROM <many tables>
WHERE <where conditions, join conditions, outer join expressions, . . .>
GROUP BY <column_list>

这种结构

AVG (<expression>) OVER (PARTITION BY <column_list>) 

计算行子集的平均值。子集由分区中的column_list确定。

所以这个类型的构造

AVG (<expression>) OVER (PARTITION BY <column_list>) / SUM (1) OVER (PARTITION BY <column_list>),

实际上是将平均值除以同一分区中的行数。 (这让我觉得有点奇怪,如果我在你的位置,我还值得调查。)它是同一个分区,因为第一个partition by中的column_list与第二个partition by中的column_list相同。实际上,所有四个分区都是相同的。

这些AVG() OVER (PARTITION BY...)构造中有两个。一个是关注“imporip”的平均值;另一方面关注“imporico”的平均值。

如果您需要,我会留给Oracle专家解释旧的外连接语法。