ratio_to_report()over()  第一个括号中就是分子,over() 括号中就是分母,分母缺省就是整个占比,至于你说的select中没有销售机型  没看懂

解决方案 »

  1.   

    比如说有三个客户,每个客户都销售三星,iphone,诺基亚机子,我要算每个客户三星销售量占该客户所销售的所有类型机子之和的百分比
      

  2.   

    那你在over 中加个partition by 客户id  不就行了么
      

  3.   

    给你个例子,你试着自己编写下。SELECT
       sales_rep_id, 
       customer_id, 
       order_total,
       ROUND(100*ratio_to_report(order_total) 
          OVER (PARTITION BY customer_id),2) pct_total
    FROM
       orders
    WHERE
       sales_rep_id = 163
    ORDER BY
       sales_rep_id, customer_id, order_id/SALES_REP_ID CUSTOMER_ID ORDER_TOTAL PCT_TOTAL                                  
    ------------ ----------- ----------- ---------                                  
             163         102    5,610.60     34.78                                  
             163         102   10,523.00     65.22                                  
             163         103       78.00       .57                                  
             163         103   13,550.00     99.43                                  
             163         105    1,926.60    100.00                                  
             163         106    5,546.60    100.00                                  
             163         117    3,878.40    100.00                                  
             163         147    1,500.80    100.00                                  
             163         149    9,055.00    100.00                                  
             163         156   68,501.00    100.00                                  
             163         157    7,110.30    100.00                                  
             163         160      969.20    100.00                                 
    12 rows selected.
      

  4.   

    with t as
     (select 'c001' cust_id, 'n01' m_type, '1' c_num
        from dual
      union all
      select 'c001' cust_id, 'n01' m_type, '2' c_num
        from dual
      union all
      select 'c001' cust_id, 'n03' m_type, '3' c_num
        from dual
      union all
      select 'c002' cust_id, 'n01' m_type, '1' c_num
        from dual
      union all
      select 'c002' cust_id, 'n02' m_type, '2' c_num
        from dual
      union all
      select 'c002' cust_id, 'n03' m_type, '3' c_num from dual)
    select m.cust_id, m.m_type, 100*round(sum(bfb),4) bfb
      from (select cust_id,
                   m_type,    
                   ratio_to_report(c_num) over(partition by cust_id) bfb
              from t) m
     group by m.cust_id, m.m_type order by cust_id,m.m_type
    给你个测试用例,你的数据存在同一个客户,同一个机型的吧,不然只要内层的sql就行了