Oracle分析函数 ratio_to_report,可以非常方便实现某列统计值占整个分组的比例。如统计某个区域销量占总销量的比重,某个月份销量占总销量的比重等。
数据库版本
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
源表数据
SQL> select * from t;
MONTH_ID AREA SALE
--------- ---- ----
201701 华北 100
201701 华南 40
201701 华东 40
201701 西北 20
201702 华北 100
201702 华南 40
201702 华东 40
201702 西北 20
--需求1 按区域统计销售额占比