日期:2014-05-17  浏览次数:20679 次

高分求教一条统计的问题
一所连锁店用到的表结构如下(比例一列现在为空)
商场编号   月份   商品号   销售量   比例(占该商场所有当月所有商品销售总量的百分比)
301               7           1             12         8%
301               7           3             50         92%
398               7           9             100
398               7           8             810
398               7           1             120
``````````````````````````````````````
`````````````````````````````````````

1。现需要根据销售量   UPDATE   比例一列
2。输出编号1商品占所有商品销售总量的百分比


请各位大哥帮帮忙,来个效率最高的算法

------解决方案--------------------
我测试了还可以,你试试看~~~

1:========sql_1===============

select tt.pId,
tt.month,
tt.proId,
round(RATIO_TO_REPORT(tt.quantity)
over(partition by tt.pId, tt.month) * 100,
2) || '% ' as percent
from tablename tt;


===============result_1================

PID MONTH PROID PERCENT
---------- ---------- ---------- -----------------------------------------
301 7 1 19.35%
301 7 3 80.65%
398 7 9 9.71%
398 7 8 78.64%
398 7 1 11.65%


2:=========sql_2==============

select *
from (select tt.proId,
round(RATIO_TO_REPORT(sum(tt.quantity)) over() * 100, 2) || '% ' as percent
from tablename tt
group by tt.proId)
where proId = 1;


==============result_2=================

PROID PERCENT
---------- -----------------------------------------
1 12.09%

------解决方案--------------------
create table tt (id varchar2(10),mon number(2),code varchar2(1),account number(10),scale number(10,2));
insert into tt values( '301 ',7, '1 ',12,0);
insert into tt values( '301 ',7, '3 ',50,0);
insert into tt values( '398 ',7, '9 ',100,0);
insert into tt values( '398 ',7, '8 ',810,0);
insert into tt values( '398 ',7, '1 ',120,0);

update tt t set scale=account/(select sum(account) from tt where t.id=id and t.mon=mon);

------解决方案--------------------
SELECT 商品编号,销售量, SUM (销售量) OVER (PARTITION BY 商场编号) 商场销售量合计,
ratio_to_report (ship_no) OVER (PARTITION BY 商场编号) AS 占商场销售量比例,
月份
FROM 表

这个是算法是商品所在商场的比例(小数)

此类问题用分析函数解决很方便,而且效率很高.